最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

AddReplace object from array of objects JavaScript - Stack Overflow

programmeradmin3浏览0评论

I want to Add/Replace the object from array of objects by testing some properties against array object

  • Replace object if name and id matches

  • Add object to the array if name and id does not match from array objects

I am using below code, its working fine but I am not sure is it a good solution.

let arr = [{name: 'test1', id:1, data: {a:1} }, {name:'test2', id:2, data: {a:2}}]
let obj = {name:'test3', id:3, data: {a:3}}
let itemFound = false;
let newArr = arr.map((item)=>{
  let test = item.name === obj.name && item.id === obj.id;
  if(test){
	itemFound = true;
  }
  return test ? obj : item;
});

if(!itemFound){
	newArr.push(obj);
}
console.log(newArr)

I want to Add/Replace the object from array of objects by testing some properties against array object

  • Replace object if name and id matches

  • Add object to the array if name and id does not match from array objects

I am using below code, its working fine but I am not sure is it a good solution.

let arr = [{name: 'test1', id:1, data: {a:1} }, {name:'test2', id:2, data: {a:2}}]
let obj = {name:'test3', id:3, data: {a:3}}
let itemFound = false;
let newArr = arr.map((item)=>{
  let test = item.name === obj.name && item.id === obj.id;
  if(test){
	itemFound = true;
  }
  return test ? obj : item;
});

if(!itemFound){
	newArr.push(obj);
}
console.log(newArr)

Share Improve this question asked Feb 12, 2018 at 9:46 Dipak TelangreDipak Telangre 1,9934 gold badges23 silver badges49 bronze badges 4
  • 1 If it works, perhaps Codereview is a better match? – mplungjan Commented Feb 12, 2018 at 9:52
  • You can use Array prototype with a custom method. @Dipak – Kishor Velayutham Commented Feb 12, 2018 at 9:54
  • Since it works, why not just wrap it in a function? – gurvinder372 Commented Feb 12, 2018 at 9:59
  • 1 What can be the reason for down-vote. Instead it would be greater help if suggest edit. – Dipak Telangre Commented Feb 12, 2018 at 10:32
Add a comment  | 

3 Answers 3

Reset to default 17

You could look for the index and update the array, if found or push the object.

var array = [{ name: 'test1', id: 1, data: { a: 1 } }, { name: 'test2', id: 2, data: { a: 2 } }],
    object = { name: 'test3', id: 3, data: { a: 3 } },
    index = array.findIndex(({ name, id }) => name === object.name && id === object.id);

if (index === -1) {
    array.push(object);
} else {
    array[index] = object;
}

console.log(array);

Wraping @Nina Scholz 's answer in a function


objectReplacer(arrayOfObjects, newObject) {

      let index = arrayOfObjects.findIndex((value) => value === newObject.value);

      if (index === -1) {
        arrayOfObjects.push(newObject);
      } else {
        arrayOfObjects[index] = newObject;
      }
    }

You can use Array.prototype.find()

let arr = [{name: 'test1', id:1, data: {a:1} }, {name:'test2', id:2, data: {a:2}}]
let obj = {name:'test3', id:3, data: {a:3}}

const newArr = arr.slice();
const existingObj = newArr.find(item => item.name === obj.name && item.id === obj.id);

if(existingObj) {
    Object.assign(existingObj, obj);
} else {
    newArr.push(obj)
}

console.log(newArr)

发布评论

评论列表(0)

  1. 暂无评论