0

What is the best way to reorganize array into output? I need to merge all value keys (whether array or not) into objects sharing the same name key. There is something similar here, but that doesn't answer my question because I have arrays as well.

var array = [{
  VULN: [{random1:"asd11",random2:"asd12"}, {random3:"asd23",random4:"asd24"}]
}, {
  VULN: [{random5:"asd35",random6:"asd36"}, {random7:"asd47",random8:"asd43"}]
}, {
  VULN: [{random9:"asd55",random10:"asd51"}, {random11:"asd56",random12:"asd63"}]
}];

to

VULN=[{random1:"asd11",random2:"asd12"}, {random3:"asd23",random4:"asd24"},{name:"asd3",value:"asd3"}, {random5:"asd35",random6:"asd36"}, {random7:"asd47",random8:"asd43"}, {random9:"asd55",random10:"asd51"}, {random11:"asd56",random12:"asd63"}]

3 Answers 3

1

Here is what you want:

var array = [{
    VULN: [{ name: "asd1", value: "asd1" }, { name: "asd2", value: "asd2" }]
}, {
    VULN: [{ name: "asd3", value: "asd3" }, { name: "asd4", value: "asd4" }]
}, {
    VULN: [{ name: "asd5", value: "asd5" }, { name: "asd6", value: "asd6" }]
}];

console.log(array.map(x => x.VULN).flat())

Sign up to request clarification or add additional context in comments.

Comments

0

Use flatMap:

array.flatMap(x => x.VULN);

Comments

0

Use lodash

lodash.flatten(array.map(x => x.VULN))

Comments

Your Answer

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.