1

I have an array of objects that I would like to filter, and then return the filtered array but compare with another array to see if it needs to be replaced.

For example, I want to filter allItems by category_id = 2, and then based on the newItems array, I want to return the filtered array but with the update item in newItems (if it exists).

allItems = [
  {
    'id': 1,
    'category_id': 2,
    'text': 'old',
    'child': [
      {
        'id': 2,
        'category_id': 2,
        'text': 'old'
      }
    ]
  },
  {
    'id': 3,
    'category_id': 3,
    'text': 'old'
  }
]


newItems = [
  {
    'id': 2,
    'category_id': 2,
    'text': 'new'
  }
]

So I want to return:

results = [
  {
    'id': 2,
    'category_id': 2,
    'text': 'new'
  },
  {
    'id': 3,
    'category_id': 2,
    'text': 'old'
  },
]

Right now, I have the filtering part but I'm not sure how to compare to another array and replace with the updated item.

return this.allItems.filter( (item) => {
  return item.category_id == 2
})
1

1 Answer 1

2

Use filter followed by .map, to replace items (if needed) with the new item:

const allItems = [
  {
    'id': 1,
    'category_id': 1,
    'text': 'old'
  },
  {
    'id': 2,
    'category_id': 2,
    'text': 'old'
  },
  {
    'id': 3,
    'category_id': 2,
    'text': 'old'
  }
];
const newItems = [
  {
    'id': 2,
    'category_id': 2,
    'text': 'new'
  }
];

const newItemsById = newItems.reduce((a, item) => {
  a[item.id] = item;
  return a;
}, {});

const output = allItems
  .filter(({ category_id }) => category_id === 2)
  .map((item) => newItemsById[item.id] ? newItemsById[item.id] : item);
console.log(output);

Also note that your 'id': <number> key-value pairs in your allItems need to be followed by commas, when followed by another key-value pair, for the syntax to be valid.

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

2 Comments

What if the item I'm trying to replace is nested in another one? I revised my example.
Just flatten the array of objects first? Use a recursive function if necessary

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.