3

Using the value from an array of object I'm trying to generate an another array of objects with a different structure than that of the original array.

In the following example arrays you can see that from the key:value pairs the key is changing between the Source array and Result array but the values are same. I had tried some code but not worthwhile to share. What logic or method can be used in this case to generate the object structure mentioned below

Source array

[
 {
 "node": {
 "Charecter": "Hulk",
 "Publisher": "Marvel",
 "Movie": "Avengers"
 }
 },
 {
 "node": {
 "Charecter": "Wolverine",
 "Publisher": "Marvel",
 "Movie": "X-Men"
 }
 },
 {
 "node": {
 "Charecter": "Superman",
 "Publisher": "DC",
 "Movie": "Man of steel"
 }
 }
]

Result array

[
 {
 "Franchise": "Marvel",
 "Data": [
 {
 "Lead": "Hulk",
 "In": "Avengers"
 },
 {
 "Lead": "Wolverine",
 "In": "X-Men"
 }
 ]
 },
 {
 "Franchise": "DC",
 "Data": [
 {
 "Lead": "Superman",
 "In": "Man of steer"
 }
 ]
 },
]
norbitrial
15.2k10 gold badges39 silver badges66 bronze badges
asked Mar 28, 2020 at 17:51

2 Answers 2

2

Using .reduce() and .find() combination you can achieve the goal.

Try the following:

const data = [{ "node": { "Charecter": "Hulk", "Publisher": "Marvel", "Movie": "Avengers" } }, { "node": { "Charecter": "Wolverine","Publisher": "Marvel","Movie": "X-Men" } }, {"node": { "Charecter": "Superman", "Publisher": "DC","Movie": "Man of steel" } }];
const result = data.reduce((a, c) => {
 const found = a.find(e => e.Franchise === c.node.Publisher);
 
 if (found) found.Data.push({ Lead: c.node.Charecter, In: c.node.Movie });
 else a.push({ Franchise: c.node.Publisher, Data: [{ Lead: c.node.Charecter, In: c.node.Movie }] });
 return a;
}, []);
console.log(result)

I hope this helps!

answered Mar 28, 2020 at 17:56
Sign up to request clarification or add additional context in comments.

1 Comment

That was the straight answer .. Thanks it works fine
2

You can do it with Array#reduce method where group everything within an object and extract object values using Object.values method.

const data = [{"node":{"Charecter":"Hulk","Publisher":"Marvel","Movie":"Avengers"}},{"node":{"Charecter":"Wolverine","Publisher":"Marvel","Movie":"X-Men"}},{"node":{"Charecter":"Superman","Publisher":"DC","Movie":"Man of steel"}}]"
const result = Object.values(data.reduce((obj, { node: { Publisher: p, ...rest }}) => {
 // define key if not defined
 obj[p] = obj[p] || { Franchise: p, Data: [] };
 // push to the array
 obj[p].Data.push(rest);
 return obj;
}, {}));
console.log(result)

answered Mar 28, 2020 at 18:01

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.