简体   繁体   English

如果对象与新数组不匹配,则从前一个数组中删除对象

[英]Remove objects from previous array if they don't match the new one

I am trying to remove the values that don't match in the previous array to the values in the new array.我正在尝试将前一个数组中不匹配的值删除到新数组中的值。

var previous = [
    {m: "195/50 15"},
    {m: "195/50 16"},
    {m: "195/55 15"},
    {m: "195/55 16"},
    {m: "195/55 20"}
];
var newOne = [
    {m: "195/55 15"},
    {m: "195/55 16"},
    {m: "195/55 20"}
];

console.log( previous.filter((obj) => obj['m'] !== newOne['m']) );

And if the new array is empty, then the filter should return an empty array.如果新数组为空,则过滤器应返回一个空数组。

The method I am trying above doesn't work.我上面尝试的方法不起作用。

Try using some to determine if the value is in the other array:尝试使用 some 来确定该值是否在另一个数组中:

 var previous = [ {m: "195/50 15"}, {m: "195/50 16"}, {m: "195/55 15"}, {m: "195/55 16"}, {m: "195/55 20"} ]; var newOne = [ {m: "195/55 15"}, {m: "195/55 16"}, {m: "195/55 20"} ]; const result = previous.filter(p => { return newOne.some(n => nm === pm); }) console.log(result);

Comparison should be like this对比应该是这样的


var previous = [
    {m: "195/50 15"},
    {m: "195/50 16"},
    {m: "195/55 15"},
    {m: "195/55 16"},
    {m: "195/55 20"}
];
var newOne = [
    {m: "195/55 15"},
    {m: "195/55 16"},
    {m: "195/55 20"}
];

console.log( previous.filter((obj, i) => newOne[i] && obj['m'] !== newOne[i]['m']) );

In your code you are using filter function that runs through objects inside previous array and compares each object value with newOne['m'] which returns undefined在您的代码中,您正在使用过滤器 function 运行前一个数组中的对象并将每个 object 值与返回undefined的 newOne['m'] 进行比较

    let newArray = []

    previous.forEach(pobj => {
      newOne.forEach(nobj => {
        if(pobj['m'] === nobj['m']){
          newArray.push(pobj)
        }
      })
    })

    console.log(newArray)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从数组中删除所有没有特定属性的对象? - How to remove from an array all objects that don't have a certain property? 正则表达式不匹配以前的比赛? - Regex don't match previous matches? 如何从与参数不匹配的对象中删除值 - How to remove values from an object that don't match parameters 如何从与另一个 id 数组匹配的数组中删除对象 - How to remove objects from an array which match another array of ids 匹配对象数组中的对象并删除 - match an object in an array of objects and remove 从数组中删除以前的值并用新值更新 - Remove previous values from array and update with new ones 试图比较两个对象的 arrays 并从一个数组中删除匹配的元素并返回它,目前只删除第一个匹配项 - Trying to compare two arrays of objects and remove matching elements from one array and return it, currently only removing first match 通过对象数组进行映射,甚至通过与其他对象相比没有一两个键的对象进行映射 - Map through array of objects even through objects that don't have one or two keys compared to other objects 如何使用由先前对象数组的两个键组成的键条目创建新的对象数组 - How to create new array of objects with key entrie made from two keys of previous array of objects D3.js:在上一个过渡完成之前不要开始新的过渡吗? - D3.js: Don't start new transition until the previous one has finished?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM