我有一个JSON,如下所示
[
{
"name": "Mike",
"incentives": "23.45",
"id": "1"
},
{
"name": "Larsen",
"incentives": "34.78",
"id": "2"
},
{
"name": "Steve",
"incentives": "26.78",
"id": "3"
}
]
如果JSON对象中不存在ID,我需要将新的JSON对象推送到JSON数组中
以这种方式尝试并工作,这样做的任何更好的方式
$(document).ready(function() {
var idsarray = [];
var test = [
{
"name": "Mike",
"incentives": "23.45",
"id": "1"
},
{
"name": "Larsen",
"incentives": "34.78",
"id": "2"
},
{
"name": "Steve",
"incentives": "26.78",
"id": "3"
}
];
for(var i=0;i<test.length;i++)
{
idsarray.push(test[i].id)
}
var newobj = {
"name": "RAM",
"incentives": "56.78",
"id": "4"
}
var newid = newobj.id;
if(jQuery.inArray(newid, idsarray) !== -1)
{
test.push(newobj)
}
alert(test.length)
});
答案 0 :(得分:1)
inArray
很慢。如果您执行一次,这绝不是最糟糕的事情,但是在原始数组上使用find
或some
会更好:
if (!test.some(o => o.id == newid)) {
test.push(newobj)
}
但是,如果您经常这样做,那么更好的主意是创建一个集合,因为检查集合中的成员资格非常快:
let idset = new Set();
...
if (!idset.has(newid)) {
test.push(newobj);
idset.add(newid);
}
您还可以使用Map
(甚至是普通的旧对象)将两者结合起来:
let test = new Map();
...
if (!test.has(newid)) {
test.set(newobj);
}
您可以使用Map
从test.values()
获取数组。
答案 1 :(得分:1)
您的条件不正确。应该为jQuery.inArray(newid, idsarray) === -1
,因为如果jQuery.inArray(newid, idsarray)
中没有-1
,则操作newid
将返回idsarray
。因此,您需要检查idsarray
中的唯一出现。
您也可以使用indexOf()
操作。 idsarray.indexOf(newid) === -1
。
OP的代码
$(document).ready(function() {
var idsarray = [];
var test = [{
"name": "Mike",
"incentives": "23.45",
"id": "1"
},
{
"name": "Larsen",
"incentives": "34.78",
"id": "2"
},
{
"name": "Steve",
"incentives": "26.78",
"id": "3"
}
];
for (var i = 0; i < test.length; i++) {
idsarray.push(test[i].id)
}
var newobj = {
"name": "RAM",
"incentives": "56.78",
"id": "4"
}
var newid = newobj.id;
if (jQuery.inArray(newid, idsarray) === -1) {
test.push(newobj)
}
alert(test.length)
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
使用indexOf()
$(document).ready(function() {
var idsarray = [];
var test = [{
"name": "Mike",
"incentives": "23.45",
"id": "1"
},
{
"name": "Larsen",
"incentives": "34.78",
"id": "2"
},
{
"name": "Steve",
"incentives": "26.78",
"id": "3"
}
];
for (var i = 0; i < test.length; i++) {
idsarray.push(test[i].id)
}
var newobj = {
"name": "RAM",
"incentives": "56.78",
"id": "4"
}
var newid = newobj.id;
if (idsarray.indexOf(newobj) === -1) {
test.push(newobj)
}
alert(test.length)
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
答案 2 :(得分:0)
您可以避免使用包含对象array
的另一个id
。您可以使用本机函数Array.some来检查是否存在并相应地进行推送。
var test = [{"name":"Mike","incentives":"23.45","id":"1"},{"name":"Larsen","incentives":"34.78","id":"2"},{"name":"Steve","incentives":"26.78","id":"3"}];
var newobj = {"name":"RAM","incentives":"56.78","id":"4"};
if(!test.some(({id}) => id == newobj.id)) test.push(newobj);
console.log(test);
此外,如果您的推送操作非常频繁,则每次循环都会影响性能。您可以保留ID的ids
或map
而不是set
的数组,因为搜索的复杂度为O(1)。
var test = [{"name":"Mike","incentives":"23.45","id":"1"},{"name":"Larsen","incentives":"34.78","id":"2"},{"name":"Steve","incentives":"26.78","id":"3"}];
var newobj = {"name":"RAM","incentives":"56.78","id":"4"};
var ids = test.reduce((a, {id}) => Object.assign(a, {[id]:id}), {});
if(!ids[newobj.id]) {
test.push(newobj);
ids[newobj.id] = newobj.id;
}
console.log(test);