kartik
kartik

Reputation: 294

Add a property to array of object based on the existence of value in another array

I have an array to check with main array as,

const arrayToCheck = ['a', 'b', 'c', 'd'];

Here is the Main array,

const mainArray = [ {name:'alex', code: 'c'}, 
                    {name:'jack', code: 'd'}, 
                    {name:'john', code: 'n'}, 
                    {name:'mike', code: 'l'}
                  ]

I want to add a status property with key of either 'enable' or 'disable' to the mainArray based on the values in arrayToCheck.

So the output should be

[ {name:'alex', code: 'c', status: 'enable'}, 
  {name:'jack', code: 'd', status: 'enable'}, 
  {name:'john', code: 'n', status: 'disable'}, 
  {name:'mike', code: 'l', status: 'disable'}
]

I have tried map and some to get the desired output but that did not work out,

Here is what i have tried,

const output = this.mainArray.map( (fil, i) => {
          return arrayToCheck.some( s => {
            if (s === fil.Code) {
              this.mainArray[i].Status = 'enable'
            } else {
              this.mainArray[i].Status = 'disable'
            }
          })
        });

Upvotes: 1

Views: 54

Answers (3)

Nenad Vracar
Nenad Vracar

Reputation: 122037

You could use map method with includes and spread syntax .... This solution returns new array of objects and doesn't modify original array.

const arrayToCheck = ['a', 'b', 'c', 'd'];
const mainArray = [ {name:'alex', code: 'c'}, {name:'jack', code: 'd'}, {name:'john', code: 'n'}, {name:'mike', code: 'l'}]

const result = mainArray.map(e => ({...e, status: arrayToCheck.includes(e.code) ? "enable" : "disable"}))
console.log(result)

Upvotes: 3

T.J. Crowder
T.J. Crowder

Reputation: 1074168

A couple of things there:

  1. JavaScript is case-sensitive. Code and code are not the same property.

  2. You're not returning anything from your map callback, so map isn't the right tool. If you just want to loop through, use a loop or forEach.

  3. Use the return value of some, rather than doing the if within its callback. But, here you don't need some, because you can use includes.

Assuming you want to do what you did in your question (modify the existing objects, rather than creating new ones):

const arrayToCheck = ['a', 'b', 'c', 'd'];

const mainArray = [ {name:'alex', code: 'c'}, {name:'jack', code: 'd'}, {name:'john', code: 'n'}, {name:'mike', code: 'l'}];

mainArray.forEach(entry => {
    entry.status = arrayToCheck.includes(entry.code) ? "enable" : "disable";
});

console.log(mainArray);

If you wanted to create new objects, Nenad's answer shows you how to do that. But I didn't get that from your question.

Upvotes: 2

Artem Arkhipov
Artem Arkhipov

Reputation: 7455

Here is easy to understand code:

    const arrayToCheck = ['a', 'b', 'c', 'd'];
    const mainArray = [ {name:'alex', code: 'c'}, {name:'jack', code: 'd'}, {name:'john', code: 'n'}, {name:'mike', code: 'l'}]    
    const output = mainArray.map(obj => {
            obj.status = arrayToCheck.indexOf(obj.code) < 0 ? 'disable' : 'enable';
            return obj;
        })
    console.log(output);

Upvotes: 1

Related Questions