检查数组中其他对象的子对象中是否存在对象

时间:2021-12-28 07:32:40

I have kind of this Array :

我有这种数组:

array = [
     {
      a:0, 
      b:{x:1,y:2} 
     },
     {
      c:0,
      b:{x:3,y:2}},
    ]

And I have a new object like this

我有一个像这样的新对象

{
 e:0, 
 b:{x:2,y:5}
}

I want to test whether the objects in array have the same nested b object or not, if so I'll push the new object there, if it already exists, I'll replace the whole object with the newer one. Any help how could I achieve that please (Lodash, ES6..), I'm using Typescript btw.

我想测试数组中的对象是否具有相同的嵌套b对象,如果是这样我将推送新对象,如果它已经存在,我将用更新的对象替换整个对象。任何帮助我怎么能实现这一点(Lodash,ES6 ..),我正在使用Typescript btw。

2 个解决方案

#1


1  

You can simply use Array.prototype.some() method with your array to test if there's any matching b object with the searched object, your code will be:

您可以简单地将Array.prototype.some()方法与您的数组一起使用来测试是否有与搜索对象匹配的b对象,您的代码将是:

function checkIfObjectExists(array, obj) {
  return array.some(function(element) {
    return (typeof obj.b === typeof element.b) && JSON.stringify(obj.b) === JSON.stringify(element.b);
  });
}

This is a working snippet:

这是一个工作片段:

function checkIfObjectExists(array, obj) {
  return array.some(function(element) {
    return (typeof obj.b === typeof element.b) && JSON.stringify(obj.b) === JSON.stringify(element.b);
  });
}

array = [{
    a: 0,
    b: {
      x: 1,
      y: 2
    }
  },
  {
    c: 0,
    b: {
      x: 3,
      y: 2
    }
  },
];

var searched = {
  e: 0,
  b: {
    x: 2,
    y: 5
  }
};


//Check an existing userId
console.log(checkIfObjectExists(array, searched));

//Check a non existing userId
console.log(checkIfObjectExists(array, {
  a: 0,
  b: {
    x: 1,
    y: 2
  }
}));

#2


1  

The following IsExists() retun true if same object already exists and return false if same object not exists.

如果同一对象已存在,则以下IsExists()返回true,如果同一对象不存在则返回false。

function IsExists(YourList,Object ) {
        var i;
        for (i = 0; i < YourList.length; i++) {
            if (YourList[i].c === Object .c && YourList[i].b === Object .b) {
                return true;
            }
        }

        return false;
    }

call you function like:

叫你的功能如下:

    var NewObject={
     e:0, 
     b:{x:2,y:5}
    }

if (!IsExists(array ,NewObject.b))
{
array .push(NewObject);
}

if the IsExists(array ,NewObject.b)return false then push this object into array

如果IsExists(array,NewObject.b)返回false,则将此对象推送到数组中

#1


1  

You can simply use Array.prototype.some() method with your array to test if there's any matching b object with the searched object, your code will be:

您可以简单地将Array.prototype.some()方法与您的数组一起使用来测试是否有与搜索对象匹配的b对象,您的代码将是:

function checkIfObjectExists(array, obj) {
  return array.some(function(element) {
    return (typeof obj.b === typeof element.b) && JSON.stringify(obj.b) === JSON.stringify(element.b);
  });
}

This is a working snippet:

这是一个工作片段:

function checkIfObjectExists(array, obj) {
  return array.some(function(element) {
    return (typeof obj.b === typeof element.b) && JSON.stringify(obj.b) === JSON.stringify(element.b);
  });
}

array = [{
    a: 0,
    b: {
      x: 1,
      y: 2
    }
  },
  {
    c: 0,
    b: {
      x: 3,
      y: 2
    }
  },
];

var searched = {
  e: 0,
  b: {
    x: 2,
    y: 5
  }
};


//Check an existing userId
console.log(checkIfObjectExists(array, searched));

//Check a non existing userId
console.log(checkIfObjectExists(array, {
  a: 0,
  b: {
    x: 1,
    y: 2
  }
}));

#2


1  

The following IsExists() retun true if same object already exists and return false if same object not exists.

如果同一对象已存在,则以下IsExists()返回true,如果同一对象不存在则返回false。

function IsExists(YourList,Object ) {
        var i;
        for (i = 0; i < YourList.length; i++) {
            if (YourList[i].c === Object .c && YourList[i].b === Object .b) {
                return true;
            }
        }

        return false;
    }

call you function like:

叫你的功能如下:

    var NewObject={
     e:0, 
     b:{x:2,y:5}
    }

if (!IsExists(array ,NewObject.b))
{
array .push(NewObject);
}

if the IsExists(array ,NewObject.b)return false then push this object into array

如果IsExists(array,NewObject.b)返回false,则将此对象推送到数组中