2

I get my data from an API which looks like this:

 desserts: [
 {
 name: ["Frozen Yohurt", "Ice cream Sandwich", "Egg"],
 calories: [159, 237, 109]
 },

My component (Vuetify Datatable) expects the data in a quite different form, like this:

 desserts: [
 { name: "Frozen Yogurt", calories: 159}, 
 { name: "Ice cream sandwich", calories: 237 },
 {name: "Egg", calories: 109 }
 ],

I was not able to solve it. This (Creating a JavaScript Object from two arrays) looks quite similar, but gives a different result. Can anyone help me?

Unmitigated
91.5k12 gold badges103 silver badges109 bronze badges
asked Aug 8, 2020 at 15:16
1
  • That link already has the answer to your question Commented Aug 8, 2020 at 15:20

5 Answers 5

1

You can make use of the index parameter in Array#map. Array#flatMap can be used so it works for any number of objects in the array.

let arr = [
 {
 name: ["Frozen Yohurt", "Ice cream Sandwich", "Egg"],
 calories: [159, 237, 109]
 }
];
let res = arr.flatMap(({name,calories}) => 
 name.map((name,idx)=>({name,calories:calories[idx]})));
console.log(res);

answered Aug 8, 2020 at 15:20
Sign up to request clarification or add additional context in comments.

1 Comment

@DataMastery No problem.
1

You can make this in one iteration per object. Just make sure the two arrays have equal lengths:

let desserts = [
 {
 name: ["Frozen Yohurt", "Ice cream Sandwich", "Egg"],
 calories: [159, 237, 109]
 }
];
let list = [];
for(let i = 0; i < desserts.length; i++){
 let obj = desserts[i];
 if(obj.name.length == obj.calories.length){
 for(let i = 0; i < obj.name.length; i++){
 list.push({name: obj.name[i], calories: obj.calories[i]});
 }
 }
}
console.log(list);

answered Aug 8, 2020 at 15:20

Comments

1

You could reduce the object's entries and map the found values along with the key as objects.

This approach takes only the first object found in the desserts array.

const
 desserts = { name: ["Frozen Yohurt", "Ice cream Sandwich", "Egg"], calories: [159, 237, 109] },
 result = Object
 .entries(desserts)
 .reduce((r, [k, values]) => values.map((v, i) => ({ ...r[i], [k]: v })), []);
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

answered Aug 8, 2020 at 15:24

Comments

0

Use Array.prototype.reduce to flatten each item to your desired structure

const obj = {
 desserts: [{
 name: ["Frozen Yohurt", "Ice cream Sandwich", "Egg"],
 calories: [159, 237, 109]
 }]
};
const result = obj.desserts.reduce((acc, { name, calories }) => {
 Array.prototype.push.apply(acc, name.map((item, idx) => ({
 name: item,
 calories: calories[idx]
 })));
 return acc;
}, []);
console.log(result);

answered Aug 8, 2020 at 15:25

Comments

0

Here is what you can do:

To convert the data into what you want is like this:

const requiredOutput = [];
 
for(let instance of desserts){
 instance.name.forEach((name, index)=>{
 requiredOutput.push({
 name: name,
 calories: instance.calories[index]
 })
 })
}
 
 
answered Aug 8, 2020 at 15:32

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.