Home > database >  JavaScript map and match array of objects with object of objects
JavaScript map and match array of objects with object of objects

Time:07-28

I have an array of objects with this structure:

[
    {
        "ticker": "SPR",
        "isExcluded": false,
    },
    {
        "ticker": "K",
        "isExcluded": false,
    },
    {
        "ticker": "TR:CA",
        "isExcluded": false,
    },
    {
        "ticker": "TEL:CA",
        "isExcluded": true,
    }
]

And an object of objects with this structure:

{
    "K": {
        "ticker": "K",
        "weight": 19.15
    },
    "SPR": {
        "ticker": "SPR",
        "weight": 34.55
    },
    "TEL:CA": {
        "ticker": "TEL:CA",
        "weight": 30.61
    },
    "TR:CA": {
        "ticker": "TR:CA",
        "weight": 15.69
    }
}

I would like to return the object of objects with the corresponding isExcluded value for each ticker added after its weight.

The expected output is adding the isExcluded value to the object as such:

{
    "K": {
        "ticker": "K",
        "weight": 19.15,
        "isExcluded": false
    },
    "SPR": {
        "ticker": "SPR",
        "weight": 34.55
        "isExcluded": false
    },
    "TEL:CA": {
        "ticker": "TEL:CA",
        "weight": 30.61,
        "isExcluded": true
    },
    "TR:CA": {
        "ticker": "TR:CA",
        "weight": 15.69,
        "isExcluded": false
    }
}

How can I achieve this? Your help will be appreciated. :)

CodePudding user response:

Here's a possible solution using reduce()

const object = { K: { ticker: "K", weight: 19.15 }, SPR: { ticker: "SPR", weight: 34.55 }, "TEL:CA": { ticker: "TEL:CA", weight: 30.61 }, "TR:CA": { ticker: "TR:CA", weight: 15.69 } };

const arr = [{ ticker: "SPR", isExcluded: false }, { ticker: "K", isExcluded: false }, { ticker: "TR:CA", isExcluded: false }, { ticker: "TEL:CA", isExcluded: true }];

const output = Object.values(object).reduce((prev, curr) => {
  prev[curr.ticker] = {
    ...curr,
    isExcluded: arr.find((arrObj) => arrObj.ticker === curr.ticker)?.isExcluded,
  };
  return prev;
}, {});
console.log(output)

another way would be to mutate the original object like so:

const object = { K: { ticker: "K", weight: 19.15 }, SPR: { ticker: "SPR", weight: 34.55 }, "TEL:CA": { ticker: "TEL:CA", weight: 30.61 }, "TR:CA": { ticker: "TR:CA", weight: 15.69 } };

const arr = [{ ticker: "SPR", isExcluded: false }, { ticker: "K", isExcluded: false }, { ticker: "TR:CA", isExcluded: false }, { ticker: "TEL:CA", isExcluded: true }];


Object.values(object).forEach(
  (obj) =>
    (obj.isExcluded = arr.find(
      (arrObj) => arrObj.ticker === obj.ticker
    )?.isExcluded)
);
console.log(object)

CodePudding user response:

u can use array of objects becuz u need to store same types of data in a array.and u can get access to them with index like this(if u sorted the same in both arrays)

firstArray[3]=   {
        "ticker": "TEL:CA",
        "isExcluded": true,
    };
secondeArray[3]= {
        "ticker": "TEL:CA",
        "weight": 30.61
    }
i


  • Related