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 5 years ago.
Improve this question
I got an array of objects of the following form:
let postArray = [
{
id = 1,
body = "some text"
},
{
id = 2,
body = "some other text"
}
]
which I want to rearange to the following form:
{
posts: {
1: {
id = 1,
body = "some text"
},
2: {
id = 2,
body = "some other text"
}
}
}
How can I do that using ES6?
You can reduce your array:
let postArray = [
{
id: 1,
body: "some text"
},
{
id: 2,
body: "some other text"
}
]
const posts = postArray.reduce((acc, b) => ({...acc, [b.id]: b}), {})
const result = { posts }
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 6 months ago.
Improve this question
I want to convert this:
data: {
"3": {
name: ["Missing data for required field."],
},
"5": {
id: ["Same id exist."],
},
}
into this:
data: [{
key: "name",
message: "Missing data for required field.",
row: 3,
},
{
key: "id",
message: "Same id exist.",
row: 5,
},]
how to achieve this with just es6 syntax
You can do this with Object.entries:
const data = {
"3": {
name: ["Missing data for required field."]
},
"5": {
id: ["Same id exist."]
}
};
Object.entries(data).map(([a, b]) => {
return {
key: Object.keys(b)[0],
message: Object.values(b)[0][0],
row: a
};
});
I've made some assumptions:
There will only be one key in each object
The value of the key will be an array with one string
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 1 year ago.
Improve this question
I have an array of objects like this :
[
{
"event" : {
"teams": "bla bla",
}
},
{
"event" : {
"teams": "bla bla",
}
},
]
How to combine all events that have the same teams value into one array like this:
[
{
"event" : [
{
"teams" : "bla bla"
},
{
"teams": "bla bla"
},
]
}
]
I can only use lodash
This would convert the first object objectArray into the other.
const combinedArray = [{ event: objectArray.map(obj => obj.event) }];
Edit: Grouping by teams
const groupedArray = [];
const teams = new Set(objectArray.map(obj => obj.event.teams).filter(n=>n));
for (const team of teams)
groupedArray.push({ event: objectArray.map(obj=>obj.event).filter(event => event.teams === team)});
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 1 year ago.
Improve this question
I have a json file:
[
{
"name": "Cocktail 1",
"ingredients": {
"rum": 12,
"coke": 48
}
}, {
"name": "Cocktail 2",
"ingredients": {
"gin": 24,
"tonic": 60
}
}]
Now I want to get a list of the keys of each "name" object. At the end there should be ths list
var mydata[0] = rum
var mydata[1] = coke
var mydata[2] = gin
var mydata[3] = tonic
and save it into an array.
What i have tried
var mydata = JSON.parse("jsonstring").ingredients;
hope this is understanable?
for each data in the array (map)
you want the ingredient part (.ingredients),
extract the keys (Object.keys)
and flatten the array (.flat())
array.map(a => a.ingredients).map(a => Object.keys(a)).flat();
You may prefer loop style. the only difference is flattening occurs with ... operator.
var results = [];
for (let a of array) {
results.push(...Object.keys(a.ingredients))
}
My proposition is :
'use strict'
const array = [
{
"name": "Cocktail 1",
"ingredients": {
"rum": 12,
"coke": 48
}
},
{
"name": "Cocktail 2",
"ingredients": {
"gin": 24,
"tonic": 60
}
}
]
const mydata = array.map((val) => {
return Object.keys(val.ingredients);
}).flat();
console.log(mydata)
// expected output: Array ["rum", "coke", "gin", "tonic"]
// Now you can get :
var mydata[0] = rum
var mydata[1] = coke
var mydata[2] = gin
var mydata[3] = tonic
Hope that help you? Thank
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))