0

I want to check if a value in array1 exists in an object in array2.

array1:

[2,5,1]

array2:

[
  { value: 1, name:  'Monday', isSelected: false }, 
  { value: 2, name: 'Tuesday', isSelected: false  }, 
  { value: 3, name: 'Wednesday', isSelected: false  },
  { value: 4, name: 'Thursday', isSelected: false  }, 
  { value: 5, name: 'Friday', isSelected: false  },
]

What I want to achieve is to check array1 against array2 property named value. If the value of an object in array2 is included in array1, the isSelected property should be updated to true. I've tried:

this.setState(prevState => ({
  ...prevState,
  array2: prevState.array2.map(el => {
    if (el.value === array2) {
      return {
        ...el,
        isSelected: !el.isSelected
      }
    }
    return el;
  })
}))
3
  • You can map array2 and extract only the values array2.map(a => a.value) and then loop through it if it contains array1 values Commented Dec 16, 2019 at 8:34
  • Try this.. if(array1.indexOf(el.value) > -1){isSelected: !el.isSelected}. Where el is each object of array2 Commented Dec 16, 2019 at 8:36
  • What if array2 starts out with isSelected properties that are true, or can that never happen? Would you want those properties to remain true, or would you want to set them to false if not in array1? Commented Dec 16, 2019 at 8:37

3 Answers 3

5

Inside the .map callback, always return, and set the isSelected property to whether array1.includes(obj.value), where obj is the object you're iterating over:

const array1 = [2, 5, 1];
const array2 = [{
    value: 1,
    name: 'Monday',
    isSelected: false
  }, {
    value: 2,
    name: 'Tuesday',
    isSelected: false
  }, {
    value: 3,
    name: 'Wednesday',
    isSelected: false
  },
  {
    value: 4,
    name: 'Thursday',
    isSelected: false
  }, {
    value: 5,
    name: 'Friday',
    isSelected: false
  },
];

const output = array2.map(obj => ({
  ...obj,
  isSelected: array1.includes(obj.value)
}));
console.log(output);

It's not entirely clear, but if isSelected properties can start out as true and you want to preserve them, despite the numbers not existing in the array1, change to isSelected: obj.selected || array1.includes(obj.value).

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

Comments

3

The best approach here is to check array2 against array1. Below is a working code snippet.

let values = [
  { value: 1, name:  'Monday', isSelected: false }, 
  { value: 2, name: 'Tuesday', isSelected: false  }, 
  { value: 3, name: 'Wednesday', isSelected: false  },
  { value: 4, name: 'Thursday', isSelected: false  }, 
  { value: 5, name: 'Friday', isSelected: false  },
]

const selectedValues = [2,5,1];

values = values.map((item) => {

  if(selectedValues.indexOf(item.value) > -1) {
    item.isSelected = true;
  }

  return item;
});

console.log(values);

And here is the link from jsbin: https://jsbin.com/nikabegobe/edit?js,console,output

Comments

0

You may also use some() and includes()

const arr1 = [2,5,1];
const arr2 = [{ value: 1, name:  'Monday', isSelected: false }, { value: 2, name: 'Tuesday', isSelected: false  }, { value: 3, name: 'Wednesday', isSelected: false  },
      { value: 4, name: 'Thursday', isSelected: false  }, { value: 5, name: 'Friday', isSelected: false  },
    ];

let isSelected = arr2.some(obj => arr1.includes(obj.value));

console.log(isSelected)

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.