Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
Below JSON Object needs to be iterated and need result as below.
[
{"Aazam":1, "Jagannath":2, "Bharath Kumar M":4 },
{"Bharath Kumar M":1, "Syad":1 },
{"Bharath Kumar M":2 }
]
output needed (can be in map):
Aazam: 1
Jagannath: 2
Bharath Kumar M: 4, 1, 2
Syad: 1
I tried with ES 6 syntax, I am able to get key and values but I am not successful in forming the array as needed.
Solution should be generic.
You can have a object as a output with the unique keys and values for each key as an array that holds the values of all similar keys.
var arr = [{
"Aazam": 1,
"Jagannath": 2,
"Bharath Kumar M": 4
},
{
"Bharath Kumar M": 1,
"Syad": 1
},
{
"Bharath Kumar M": 2
}
];
var res = arr.reduce((acc, item) => {
var keys = Object.keys(item);
keys.forEach((key) => {
if (!acc[key]) {
acc[key] = [];
}
acc[key].push(item[key]);
});
return acc;
}, {});
console.log(res);
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 days ago.
Improve this question
let filtered = [
[
"name",
"contains",
"men"
],
[
"web",
"contains",
"www.google.com"
],
[
"phoneNumber",
"contains",
"017XXXXXXXXXX"
]
];
Expected result:
let filtered = {
name: {
contains: "men"
},
web: {
contains: "www.google.com"
},
phoneNumber: {
contains: "017XXXXXXXXXX"
}
}
I need to filtered upper array elements to object elements using javascript. Here array value need to object key & value
let filteredObject = Object.create(null);
for(let filter of filtered) {
filteredObject[filter[0]] = Object.create(null);
filteredObject[filter[0]][filter[1]] = filter[2];
}
Object.create(null) may be omitted and replaced with {} if you are certain that a trusted entity is in control of the strings in the filtered array.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
Group javascript array items on value
Assuming you have a json object like:
[
{
prNumber: 20000401,
text: 'foo'
},
{
prNumber: 20000402,
text: 'bar'
},
{
prNumber: 20000401,
text: 'foobar'
},
]
Is it possible to perform a "join" on prNumber?
For example, maybe the desired output would be something like:
[
{
prNumber: 20000401,
text: [
'foo',
'foobar'
]
},
{
prNumber: 20000402,
text: [
'bar'
]
}
]
I have no code samples worth anything, so I will not post them here.
This would preferrably use vanilla javascript, but will accept a jQuery answer.
You should iterate the initial array and create new objects keyed off the prNumber. Here's how to do it using reduce (assuming you've assigned the array to a variable named orig):
var result = orig.reduce(function(prev, curr, index, arr) {
var num = curr["prNumber"];
if (!prev[num]) {
prev[num] = [];
}
prev[num].push(curr["text"]);
return prev;
}, {});
You can easily convert this into the example structure outlined in your question.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I hope it is not a stupid question. My array looks like this:
const data = [
{
pollType: "1",
pollName: "Can you help me?",
options: [
{ Options: "Yes, I can", PID: "oFnxh-NDdcP" },
{ Options: "No way!", PID: "d9A10-omlUd" }
]
}
];
But I need to have it:
const result = [
{
pollType: "1",
pollName: "Can you help me?",
option1: "Yes, I can",
pid1: "oFnxh-NDdcP",
option2: "No way!",
pid2: "d9A10-omlUd"
}
];
Please don't get angry if it is so simple to do. I highly appreciate your help and if you do a an example so myself and other people can find it very useful in a future...
var orig = [{"pollType":"1","pollName":"Can you help me?","options":[{"Options":"Yes, I can","PID":"oFnxh-NDdcP"},{"Options":"No way!","PID":"d9A10-omlUd"}]}]
var newArr = [];
orig.forEach(v => {
var newObj = {};
newObj.pollType = v.pollType;
newObj.pollName = v.pollName;
v.options.forEach((k, i) => {
newObj["options" + (i + 1)] = k.Options;
newObj["pid" + (i + 1)] = k.PID;
});
newArr.push(newObj);
});
console.log(newArr);
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I have data like:
var data = [
{
items: [
{
id: 123
},
{
id: 234
},
{
id: 123
}
]
}, {
items: [
{
id: 123
},
{
id: 234
}
]
}
]
so, I want count object deep in array inside of all data by property 'id'.
ex: data.countObject('id',123) //return 3.
and my data have about xx.000 item, which solution best?
Thanks for help (sorry for my English)
You can use reduce & forEach. Inside the reduce callback you can access the items array using curr.items where acc & curr are just parameters of the call back function. Then you can use curr.items.forEach to get each object inside items array
var data = [{
items: [{
id: 123
},
{
id: 234
},
{
id: 123
}
]
}, {
items: [{
id: 123
},
{
id: 234
}
]
}];
function getCount(id) {
return data.reduce(function(acc, curr) {
// iterate through item array and check if the id is same as
// required id. If same then add 1 to the accumulator
curr.items.forEach(function(item) {
item.id === id ? acc += 1 : acc += 0;
})
return acc;
}, 0) // 0 is the accumulator, initial value is 0
}
console.log(getCount(123))
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I've got a recursive array. So the array below can go deeper and deeper.
0: "1"
1: [
0: "2"
1: [
0: "3"
]
2: [
0: "4"
1: [
0: "5"
]
]
]
I want the output to be the path of all the values. So 123 and 1245.
How can this be done in a Javascript method?
You need a recursive method to flatten a recursive array.
Here's a pretty basic one:
var data = ["1",
[
"2",
[
"3"
],
[
"4",
[
"5"
]
]
]];
var flattened = [];
function flatten(data, outputArray) {
data.forEach(function (element){
if(Array.isArray(element)) {
flatten(element, outputArray);
} else {
outputArray.push(element);
}
});
}
flatten(data, flattened);
This should get you moving in the right direction. Good luck!
I am not sure, but what you have presented looks more like an object. In such case it is quite easy to traverse through the nested object, eg.
var object = { 0: "1",
1: {
0: "2",
1: {
0: "3"
},
2: {
0: "4",
1: {
0: "5"
}
}
}};
console.info(object);
function traverse(obj) {
obj.keys.forEach(function(key) {
if (typeof(obj[key]) === 'object') {
traverse(obj[key])
}
else {
//do something with the actual value
console.log(obj[key])
}
})
};
traverse(object)
Can you specify what did you mean with I want the output to be the path of all the values?
You could try lodash:
_.flattenDeep([1, [2, [3, [4]], 5]]);
// => [1, 2, 3, 4, 5]
https://lodash.com/docs/4.17.2#flattenDeep
but I'm not sure of your example data; you said an array but it looks (almost) like an object, in which case you'd have to first convert this to array. Again, lodash is your friend!
If you don't want to use lodash you could try something like this:
var results = [];
function flatten(arr) {
arr.forEach(function(item) {
if (Array.isArray(item)) {
flatten(item);
}
else {
results.push(item);
}
})
}
flatten(data);
Fiddle
Edit after comment by Nina Scholz
It looks like the OP has an object with keys '0', '1', '2' etc, each of which may contain a recursive array. So the code above would have to be executed on the data associated with each key and the results appended to the value of the key to get 123, 1245 etc.