0

I have an array of the objects

[
    {"id":"brand","label":"Brand","value":"value1"},    
    {"id":"waterproof","label":"Waterproof","value":"value1"},
    {"id":"diameter","label":"Diameter","value":""},
    {"id":"brand","label":"Brand","value":"value2"},
    {"id":"waterproof","label":"Waterproof","value":"value2"},
    {"id":"diameter","label":"Diameter","value":""}
]

I need to restructure it to a following structure,

[
    ["Brand", "value1", "value2"], 
    ["Waterproof", "value1","value2"], 
    ["Diameter","",""]
]

Any ideas on how I could do this using a reduce method.

Best

3
  • specify how you got from input to output Commented Sep 29, 2018 at 7:40
  • where do you get value2 from? Commented Sep 29, 2018 at 7:44
  • @NinaScholz ` {"id":"brand","label":"Brand","value":"value2"},` Commented Sep 29, 2018 at 7:45

6 Answers 6

2

Try following using Array.reduce and Object.values

let arr = [{"id":"brand","label":"Brand","value":"value1"},{"id":"waterproof","label":"Waterproof","value":"value1"},{"id":"diameter","label":"Diameter","value":""},{"id":"brand","label":"Brand","value":"value2"},{"id":"waterproof","label":"Waterproof","value":"value2"},{"id":"diameter","label":"Diameter","value":""}];

let result = Object.values(arr.reduce((a,c) => {
  if(a[c.id]) a[c.id].push(c.value);
  else a[c.id] = [c.label, c.value];
  return a;
}, {}));
console.log(result);

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

1 Comment

@BobanStanojevic - Glad to help you :)
2

Yust take a Map and collect all values.

var json = '[{"id":"brand","label":"Brand","value":"value1"},{"id":"waterproof","label":"Waterproof","value":"value1"},{"id":"diameter","label":"Diameter","value":""},{"id":"brand","label":"Brand","value":"value2"},{"id":"waterproof","label":"Waterproof","value":"value2"},{"id":"diameter","label":"Diameter","value":""}]',
    result = Array.from(JSON
        .parse(json)
        .reduce((m, { id, label, value }) => m.set(id, (m.get(id) || [label]).concat(value)), new Map)
        .values()
    );
    
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

2 Comments

It is not the expected result
@NikhilAggarwal, right. now it should return the expected result.
1

Not the best of code but i think this might be useful for you:

let data = [
        {"id":"brand","label":"Brand","value":"value1"},    
        {"id":"waterproof","label":"Waterproof","value":"value1"},
        {"id":"diameter","label":"Diameter","value":""},
        {"id":"brand","label":"Brand","value":"value2"},
        {"id":"waterproof","label":"Waterproof","value":"value2"},
        {"id":"diameter","label":"Diameter","value":""}
    ]

    let reducedData = {}
    data.forEach((row)=>{
        reducedData[row.id] ? reducedData[row.id].push(row.value) : reducedData[row.id] = [row.label, row.value]    
    })
    let newData = Object.keys(reducedData).map((id)=>{return [...reducedData[id]]})

Comments

1

You can also use a simple combo of Array.prototype.reduce and Object.keys:

const data = [{"id":"brand","label":"Brand","value":"value1"},{"id":"waterproof","label":"Waterproof","value":"value1"},{"id":"diameter","label":"Diameter","value":""},{"id":"brand","label":"Brand","value":"value2"},{"id":"waterproof","label":"Waterproof","value":"value2"},{"id":"diameter","label":"Diameter","value":""}];

const grouped = data.reduce((o, { label, value }) =>
    ({...o, [label]: [...(o[label] || []), value]}), {});

const result = Object.keys(grouped).map(label => [label, ...grouped[label]]);

console.log(result);

Comments

1

Here I use Set to create a unique list of id's then I map to create a custom array based on those id's. This is done by using find to get the label, and filter to get all the items that are related to the id. I map again on the filter to just return the value of the object.

let items = [
    {"id":"brand","label":"Brand","value":"value1"},    
    {"id":"waterproof","label":"Waterproof","value":"value1"},
    {"id":"diameter","label":"Diameter","value":""},
    {"id":"brand","label":"Brand","value":"value2"},
    {"id":"waterproof","label":"Waterproof","value":"value2"},
    {"id":"diameter","label":"Diameter","value":""}
]

let result = [...new Set(items.map(i => i.id))]
  .map(id => {
    return [
      items.find(i => i.id == id).label,
      ...items.filter(i => i.id == id).map(i => i.value)
    ]
  })

console.log(result)

Comments

0

In case the JSON Array is a very long one from some API

var data = [
    {"id":"brand","label":"Brand","value":"value1"},    
    {"id":"waterproof","label":"Waterproof","value":"value1"},
    ...... //some JSON array form some API
];
var json = JSON.stringify(data);

result = Array.from(JSON
    .parse(json)
    .reduce((m, { id, label, value }) => m.set(id, (m.get(id) || [label]).concat(value)), new Map)
    .values()
);
console.log(result);

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.