【问题标题】:how to use .include() method to check the value which is in a json inside array如何使用 .include() 方法检查数组内 json 中的值
【发布时间】:2017-10-07 09:38:00
【问题描述】:

我想将我的 JSON 数组中特定键的值与新值进行比较,以检查该值是否存在。

例如,我有一个数组:

[
    { name: abc, num: 121212 },
    { name: bcd, num: 21212 },
    { name: def, num: 111222 }
]

现在出现了一个我要检查的新值。这个名字是不是已经存在了?如果是,那么我只想更新数字,如果不是,那么我想将对象推送到数组中。

这是我的代码:

if ((Dnum.num).includes(number)) {
    console.log("inside if");
    console.log(Dnum.indexOf(number)); 
} else {
    Dnum.push({num:number,
        lat:lat,
        lng:lng,
        name:name
    });
}

【问题讨论】:

  • 这种情况下也可以使用.some(),详见MDN
  • 您的阵列完全关闭,缺少 " 和 ,

标签: javascript arrays json


【解决方案1】:

好吧,你的问题(如果我理解正确的话)是你想使用includes(),但你真正想要完成的事情与方法的作用并不对应。您想查找数组中是否已经存在具有特定名称的对象,而不是它是否包含已知元素。像这样的:

var data = [{name: 'abc', num: 121212}, {name: 'bcd', num: 21212}, {name: 'def', num: 111222}];

function addOrUpdate(newElement, data) {
    var i;
    for (i = 0; i < data.length; i++) {
        if (data[i].name == newElement.name) {
            data[i] = newElement;
            return;
        }
    }
    data.push(newElement);
}

addOrUpdate({name: 'bcd', num: 131313}, data);
console.log(data);
addOrUpdate({name: 'new', num: 131313}, data);
console.log(data);

【讨论】:

    【解决方案2】:

    问题:

    实际上.includes().indexOf() 方法不适用于objects,它们应该与stringsNumbers 的数组一起使用,因为它们使用strict equality 来比较元素和对象可以'不能这样比较,所以需要自己实现这个逻辑。

    解决方案:

    需要检查数组中是否已经存在与搜索到的name匹配的对象,更新该对象的num值,否则如果没有匹配到搜索到的名称,则将新对象推送到数组中:

    if (arr.some(function(obj) {
        return obj.name === searchedVal.name;
      })) {
      arr.forEach(function(el, index) {
        if (el.name === searchedVal.name) {
          el.num += searchedVal.num;
          found = true;
        }
      });
    } else {
      arr.push(searchedVal);
    }
    

    演示:

    var arr = [{
      name: "abc",
      num: 121212
    }, {
      name: "bcd",
      num: 21212
    }, {
      name: "def",
      num: 111222
    }];
    
    var searchedVal = {
      name: "abc",
      num: 5
    };
    
    if (arr.some(function(obj) {
        return obj.name === searchedVal.name;
      })) {
      arr.forEach(function(el, index) {
        if (el.name === searchedVal.name) {
          el.num += searchedVal.num;
          found = true;
        }
      });
    
    } else {
      arr.push(searchedVal);
    }
    console.log(arr);

    如果不想使用.some()方法,可以这样:

    var searchedVal = {
      name: "abc",
      num: 5
    };
    var found = false;
    arr.forEach(function(el, index) {
      if (el.name === searchedVal.name) {
        el.num+= searchedVal.num;
        found = true;
      }
    });
    
    if (!found) {
      arr.push(searchedVal);
    }
    

    【讨论】:

      【解决方案3】:

      使用Array.prototype.find():

      var res = Dnum.find(function (item) {
        return item.num === number;
      });
      
      if (res) {
        console.log("inside if");
        console.log(res);
        res.num = number; 
      } else {
      Dnum.push({
        num:number,
            lat:lat,
            lng:lng,
            name:name
        });
      }

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-11-29
        • 2018-05-13
        • 1970-01-01
        • 1970-01-01
        • 2014-09-30
        • 1970-01-01
        相关资源
        最近更新 更多