0

I have a list of object looks like this, and i want to turn it to a list of values by id.

Original = [
    {id: 1, value: 12.2494, time: "00:00:00.14"},
    {id: 1, value: 4.5141, time: "00:00:01.138"},
    {id: 1, value: 2.85930, time: "00:00:02.138"},
    {id: 1, value: 1.0364, time: "00:00:03.146"},
    {id: 2, value: 4.3510, time: "00:09:15.157"},
    {id: 2, value: 3.90, time: "00:09:16.115"},
    {id: 2, value: 3.544, time: "00:09:17.116"},
    {id: 2, value: 3.247, time: "00:09:18.157"}
]

Expected outcome

data[1]={value:[12.494, 4.5141...],time: ["00:00:00.14","00:00:01.138"]...} 
data[2]={value:[4.3510, 3.90...],time: ["00:09:15.157","00:09:16.115"]...}

I have tried something like this, but only returns one value

var data= {};
original.forEach(function(item) {
    var id = item.id;
    data[id] = {
        value:[],
        time:[]
    }
    data[id].value.push(item['value']);
    data[id].time.push(item['time']);
})
0

2 Answers 2

2

You can use the function reduce from Arrays.

Check the current id within the accumulator;

If it exists push to the already stored values and times

Else create a template object and push values and times

var Original=[{id: 1, value: 12.2494,  time: "00:00:00.14"},{id: 1, value: 4.5141, time: "00:00:01.138"},{id: 1, value: 2.85930,  time: "00:00:02.138"},{id: 1, value: 1.0364,  time: "00:00:03.146"},{id: 2, value: 4.3510,  time: "00:09:15.157"},{id: 2, value: 3.90, time: "00:09:16.115"},{id: 2, value: 3.544, time: "00:09:17.116"},{id: 2, value: 3.247,  time: "00:09:18.157"}];

var data = Original.reduce((a, c) => {
  var current = (a[c.id] || (a[c.id] = {value: [], time: []}));
  current.value.push(c.value);
  current.time.push(c.time);
  
  return a;
}, {});

console.log(data);
.as-console-wrapper { max-height: 100% !important; top: 0; }

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

1 Comment

Works perfectly fine. Thanks.
0

Original answer works great, but if you have an irrational aversion to using push (if an object is only created and mutated in a closure, does it really change?):

const original = [
    {id: 1, value: 12.2494, time: "00:00:00.14"},
    {id: 1, value: 4.5141, time: "00:00:01.138"},
    {id: 1, value: 2.85930, time: "00:00:02.138"},
    {id: 1, value: 1.0364, time: "00:00:03.146"},
    {id: 2, value: 4.3510, time: "00:09:15.157"},
    {id: 2, value: 3.90, time: "00:09:16.115"},
    {id: 2, value: 3.544, time: "00:09:17.116"},
    {id: 2, value: 3.247, time: "00:09:18.157"}
]

const data = original.reduce((x, y) => x[y.id]
    ? Object.assign({}, x, {
        [y.id]: {
            value: x[y.id]
                .value
                .concat(y.value),
            time: x[y.id]
                .time
                .concat(y.time)
        }
    })
    : Object.assign({}, x, {
        [y.id]: {
            value: [y.value],
            time: [y.time]
        }
    }), {})

Or if there are holes in the data and you don't want to add undefined, reduce to unique ids, map over that list, filter original list, reduce to merge:

    const unoriginal = [
    {id: 1, value: 12.2494, time: "00:00:00.14"},
    {id: 1, value: 4.5141, time: "00:00:01.138"},
    {id: 1, value: 2.85930, time: "00:00:02.138"},
    {id: 1, value: 1.0364},
    {id: 2, time: "00:09:15.157"},
    {id: 2, value: 3.90, time: "00:09:16.115"},
    {id: 2, value: 3.544, time: "00:09:17.116"},
    {id: 2, value: 3.247, time: "00:09:18.157"}
]

const otherData = unoriginal.reduce((x, y) => x.includes(y.id)
    ? x
    : x.concat(y.id), []).map(x => Object.assign({}, {
    [x]: {
        value: unoriginal
            .filter(y => x === y.id)
            .reduce((z, a) => a.value
                ? z.concat(a.value)
                : z, []),
        time: unoriginal
            .filter(y => x === y.id)
            .reduce((b, c) => c.time
                ? b.concat(c.time)
                : b, [])
    }
})).reduce((x, y) => Object.assign.apply(null, [x].concat(y)))

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.