0

Im trying to aggrigate huge array of objects to single object, but my if statements replace each other:

const obj = [];
res.map((el) => {
    if (el.resource.name === "FORM01" && el.name === "cost.ttl") {
        obj[el.resource.name] = { [el.name]:  el };
    }
    if ( el.resource.name === "FORM01" && el.name === "cost.use") {
        obj[el.resource.name] = { [el.name]:  el };
    }
});

in result I want to add in

obj[el.resource.name] = {}

two fields like cost.ttl and cost.use.

3
  • A small example of the "huge array of objects" might help to solve the problem ..? Commented Aug 31, 2018 at 11:48
  • What end result are you looking for? Commented Aug 31, 2018 at 11:50
  • It was my mistake I declare obj as {}. And T.J. answer suits well. Commented Aug 31, 2018 at 13:09

3 Answers 3

1

If you're not using the result, map isn't the right tool for looping through an array. Similarly, if you're mapping strings to values (el.resource.name to an object), an array isn't the correct object type to use. Just use a plain object, or a Map.

The reason your two assignments are conflicting is that the second time the condition is true, it overwrites the first object you've assigned. Instead, create an object, then add each property to the same object as necessary.

It's not clear what end result you're really looking for, but perhaps something like:

const obj = {};       // *** Object, not array
res.forEach((el) => { // *** forEach, not map
    if (el.resource.name === "FORM01" && (el.name === "cost.ttl" || el.name === "cost.use")) {
        // *** Get the existing object if any; create and store a new one if there isn't already one there
        const entry = obj[el.resource.name] = obj[el.resource.name] || {};
        // *** Add this property to it
        entry[el.name] = el;
    }
});

or you might use for-of:

const obj = {};
for (const el of res) {
    if (el.resource.name === "FORM01" && (el.name === "cost.ttl" || el.name === "cost.use")) {
        const entry = obj[el.resource.name] = obj[el.resource.name] || {};
        entry[el.name] = el;
    }
});
Sign up to request clarification or add additional context in comments.

1 Comment

Thx very much, it really helped me. I understand that it was not correct and not very nice code. Now I know how to do it right and nicer.
0

Try following

const obj = {}; // Initialize here to an object instead of array
res.map((el) => {
    if (el.resource.name === "FORM01" && (el.name === "cost.ttl" || el.name === "cost.use")) {
        // Check for existing object, else create new object
        obj[el.resource.name] = obj[el.resource.name] || {};
        obj[el.resource.name][el.name] = el; // set the value in object
    }
});

Note, Array.map is not the correct choice over here for iteration as you are trying to loop over the array and get some value on an condition and store it. You can either use a simple for loop, or forEach.

4 Comments

At a guess: Code dumps are not useful answers (the OP is supposed to guess at what you did, and why?), and map is the wrong tool if not using the array it creates. Edit: Oh, and what Jeremy said. :-)
I believe you got downvoted because obj should be an object. obj[el.resource.name] won't work on an Array, and your code didn't fix this.
@T.J.Crowder - Agreed. Adding details
@JeremyThille - Ideally you are right, however, you can also set a property to array as well just like an object. Though I have updated my answer.
0

Avoid two if statements in a row, because the first one could affect the data tested by the second one.

Also, obj should be an Object, not an Array.

Also also, use .forEach, not .map, because you're not returning anything from the loop.

const obj = {};
res.forEach( el => {
    if (el.resource.name === "FORM01" && ["cost.ttl","cost.use"].includes(el.name)) {
        obj["FORM01"] = { [el.name]:  el };
    }
});

6 Comments

You seem to be overwriting obj["FORM01"]
So does everyone else, because this condition implies el.resource.name === "FORM01". Therefore obj[el.resource.name] === obj["FORM01"].
Thx, ill try this solution also. It much cleaner then my)
@JeremyThille - I meant you are resetting. It will only have one key at the end of loop
Yes, I understood what you mean, and I repeat my previous comment :) If el.resource.name === "FORM01" then, writing obj[el.resource.name] is exactly the same as writing obj["FORM01"]. So, I did it. This is what OP's code does.
|

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.