0

I am trying to check from array of properties which are present in array of objects and which are not.

My object is like this:

var tempObj=[{id: '1', color: 'red, blue, green', age: 27},{id: '2', color: 'black, orange, yellow', age: 75}];
var tempColor = ['red', 'yellow', 'white'];

Here tempColor array contains three elements and I need to check these elements present in tempObj. Here in tempObj color property values are comma suppurated, so I am confusing here. Basically for single element check i used to do like this

var eleExists= this.tempObj.findIndex(obj => obj.age === 27) >= 0;

But for array elements to check with comma suppurated values don't have idea to check, so seeking for some help or guidance.

1 Answer 1

1
  const data = [
    {id: '1', color: 'red, blue, green', age: 27},
    {id: '2', color: 'black, orange, yellow', age: 75},
  ]

  const colors = ['red', 'yellow', 'white']

  function getNeverUsedColors(colors, data) {
    return colors.filter(color => !data.some(obj => obj.color.split(', ').includes(color)))
  }

  console.log(getNeverUsedColors(colors, data))
  // returns ['white']
Sign up to request clarification or add additional context in comments.

7 Comments

Here includes we may not use i think, If some other word contains red like tomotored, in this scenario conflicts may come right?
@Roy I updated the answer with solution that should fit your needs.
thanks for your time..but with above solution we cant find right which are existed and which are not?
@Roy it depends on what format you need to get the checking result e.g. dictionary { [id]: boolean }, or maybe an array of booleans, list of existed-only ids etc.
@Roy please specify the desired result.
|

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.