如何通过特定条件将对象添加到数组?

时间:2018-04-11 07:39:57

标签: javascript jquery arrays object key

我试着这样:

<script type="text/javascript">
    var clubs = [ 
        {id: 1, name : 'chelsea'},
        {id: 2, name : 'city'},
        {id: 3, name : 'liverpool'}
    ];
    var newClub = {id: 4, name: 'manchester united'}
    for(var i=0; i<clubs.length; i++) {
        if(clubs[i].id!=newClub.id) {
            clubs.push(newClub);
            break;
        }
    }
    console.log(clubs);
</script>

我想添加条件。如果在俱乐部数组中不存在newClub对象的id,那么我想将该对象添加到数组

它有效

但我问。这是最好的方式吗?还是有另一种更好的方法吗?

3 个答案:

答案 0 :(得分:6)

  

它有效

不,它没有。 :-)如果第一个条目不匹配,你就会推动新俱乐部:

&#13;
&#13;
var clubs = [ 
    {id: 1, name : 'chelsea'},
    {id: 2, name : 'city'},
    {id: 3, name : 'liverpool'}
];
function pushClub(newClub) {
  for(var i=0; i<clubs.length; i++) {
      if(clubs[i].id!=newClub.id) {
          clubs.push(newClub);
          break;
      }
  }
}
var newClub = {id: 4, name: 'manchester united'}
pushClub(newClub);
pushClub(newClub);
console.log(JSON.stringify(clubs));
&#13;
.as-console-wrapper {
  max-height: 100% !important;
}
&#13;
Note that there are two id = 4 clubs.
&#13;
&#13;
&#13;

在知道是否应添加新项目之前,需要遍历整个数组。

我可能会使用stackblitz来查看该项目是否存在:

if (!clubs.some(c => c.id == newClub.id)) {
    clubs.push(newClub);
}

&#13;
&#13;
var clubs = [ 
    {id: 1, name : 'chelsea'},
    {id: 2, name : 'city'},
    {id: 3, name : 'liverpool'}
];
function pushClub(newClub) {
  if (!clubs.some(c => c.id == newClub.id)) {
    clubs.push(newClub);
  }
}
var newClub = {id: 4, name: 'manchester united'}
pushClub(newClub);
pushClub(newClub);
console.log(JSON.stringify(clubs));
&#13;
.as-console-wrapper {
  max-height: 100% !important;
}
&#13;
Note that there is only one id = 4 club.
&#13;
&#13;
&#13;

我在那里使用ES2015 +箭头功能,但您可以使用ES5传统功能:

if (!clubs.some(function(c) { return c.id == newClub.id; })) {
    clubs.push(newClub);
}

循环位于some,如果回调曾返回真值,则返回true,如果永远不返回则返回false。 (当回调返回真值时,它也会提前停止。)

如果您希望更新现有分会(如果有),我会改为使用Array#some

var existingClub = clubs.find(c => c.id == newClub.id);
if (existingClub) {
    existingClub.name = newClub.name;
} else {
    clubs.push(newClub);
}

答案 1 :(得分:1)

它不起作用,因为你在第一个循环中插入了对象。你需要检查所有元素,如果所有元素都不包含想要的id,你可以在数组顶部添加对象。

不工作代码,找到索引。

&#13;
&#13;
var clubs = [{ id: 1, name: 'chelsea' }, { id: 2, name: 'city' }, { id: 3, name: 'liverpool' }],
    newClub = { id: 4, name: 'manchester united' },
    i;
    
for (i = 0; i < clubs.length; i++) {
  if (clubs[i].id != newClub.id) {
    clubs.push(newClub);
    break;
  }
}

console.log(i, clubs);
&#13;
&#13;
&#13;

工作代码,检查所有分会是否包含新id

&#13;
&#13;
var clubs = [{ id: 1, name: 'chelsea' }, { id: 2, name: 'city' }, { id: 3, name: 'liverpool' }],
    newClub = { id: 4, name: 'manchester united' };
    
if (!clubs.some(({ id }) => id === newClub.id)) {
    clubs.push(newClub);
}

console.log(clubs);
&#13;
&#13;
&#13;

答案 2 :(得分:1)

试试这个

arr1.some功能

&#13;
&#13;
var clubs = [{
    id: 1,
    name: 'chelsea'
  },
  {
    id: 2,
    name: 'city'
  },
  {
    id: 3,
    name: 'liverpool'
  }
];
var newClub = {
  id: 4,
  name: 'manchester united'
}
let found = clubs.some(r => r.id == newClub.id)
for (var i = 0; i < clubs.length; i++) {
  if (!clubs.some(r => r.id == newClub.id)) {
    clubs.push(newClub);
  }
}
console.log(clubs);
&#13;
&#13;
&#13;