-4

I have two arrays, one with existing tags of book and another with searched results by user input value of tag to add and I need to show user tags to add that doesn't already exist in tags:

tags = [{id:1, name:'x'},{id:2, name:'y'},{id:5, name:'z'}]
search = [{id:2, name:'y'},{id:5, name:'z'},{id:15, name:'s'}]

How can I make a new array that will have search id's and other data that doesn't exist in tags array?

This is how it's worked for me in the end:

let newArray= search
  .filter(({ id }) => {
    return !tags.some(tag => {
      return tag.id == id
    })
  })
  .map(tag => {
    return (
      <Button
        key={tag.id}
      >
        {tag.name}
      </Button>
    )
  })
2

5 Answers 5

2

Using map get all the values in the object in the tags array. Run a filter on the search array and if the id is not present in the above created array , return it

var tags = [{id:1, name:'x'},{id:2, name:'y'},{id:5, name:'z'}]
var search = [{id:2, name:'y'},{id:5, name:'z'},{id:15, name:'s'}]
console.log(search.filter(({id,name})=>!tags.find(e=>e.id==id && e.name==name)))

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

1 Comment

updated question, there is many values in one item, so i need to save them.
1

I assume that the desired result in the example is {id:15}, since 15 is the only value in search that is not present in tags.

In that case, you should filter the search array, rather than the tags one. The condition is that the searched is not found in tags. The find method comes to the rescue here.

The actual code may look like this

let filtered = search.filter(searched =>
    !tags.find(tagged => tagged.id === searched.id)
)

Hope it helps - Carlos

1 Comment

ye, but i used some instead of find
1

You can use filter and find or some to do it in a single line:

const tags = [{id:1},{id:2},{id:5}];
const search = [{id:2},{id:5},{id:15}];

const result1 = search.filter(({ id }) => !tags.some(tag => tag.id === id));
const result2 = search.filter(({ id }) => !tags.find(tag => tag.id === id));

console.log(result1, result2);

If you use an older version of JavaScript, here is a version without arrow functions and destructuring:

const tags = [{id:1},{id:2},{id:5}];
const search = [{id:2},{id:5},{id:15}];

const result1 = search.filter(search => { return !tags.some(tag => { return tag.id === search.id; }); });
const result2 = search.filter(search => { return !tags.find(tag => { return tag.id === search.id; }); });

console.log(result1, result2);

6 Comments

You can use some instead of find
@adiga, you are totally right, I will update the answer
you need to add return for .some
@RTW, no you don't need that as there is no block around it so the return is implicit
without return it doesn't work for me for some reason. Added worked version in question.
|
0

I think what you want is:

search.filter(searchTag => tags.every(tag => tag.id != searchTag.id))

1 Comment

!arr.some(x => x === anything) is equivalent to arr.every(x => x !== anything) and the latter is easier to read because it doesn't rely on keeping the ! in mind for the entire rest of the expression.
0

This is how it works for me:

let newArray= search
      .filter(({ id }) => {
        return !tags.some(tag => {
          return tag.id == id
        })
      })
      .map(tag => {
        return (
          <Button
            key={tag.id}
          >
            {tag.name}
          </Button>
        )
      })

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.