我有一个数组。
[
{
tab:0,
ip:'555.222.111.555',
stid:'sth'
},
{
tab:0,
ip:'123.321.231.123',
stid:'aaa'
},
]
现在我需要
+1
添加到tab
ip
为555.222.111.555
的{{1}}。ip
为123.321.231.123
的整个对象。答案 0 :(得分:0)
这实际上是一个对象数组,而不是数组数组。
您可以这样做:
1
2
5
6
9
11
12
14
42
答案 1 :(得分:0)
或类似的东西
var adresses = [
{
tab : 0,
ip : '555.222.111.555',
stid : 'sth'
},
{
tab : 0,
ip : '123.321.231.123',
stid : 'aaa'
}
];
var results = adresses.filter((x) => x.ip != "555.222.111.555");
results.forEach((x) => {x.tab++});
console.log(results); // [ { tab: 1, ip: '123.321.231.123', stid: 'aaa' } ]
答案 2 :(得分:0)
你绝对可以尝试这样:
var a = [{
tab: 0,
ip: '555.222.111.555',
stid: 'sth'
}, {
tab: 0,
ip: '123.321.231.123',
stid: 'aaa'
}];
// search the element in the existing array.
function search(ip) {
for (var i = 0, len = a.length; i < len; i += 1) {
if (a[i].ip === ip) {
return i;
}
}
}
// adds an ip to the global storage.
function add(ip, stid) {
debugger;
var index = search(ip);
if (index !== undefined) {
a[index].tab += 1;
} else {
a.push({
tab: 1,
ip: ip,
stid: stid
});
}
}
// remove the ip history from the storage.
function remove(ip) {
var index = search(ip);
if (index !== undefined) {
a.splice(index, 1);
}
}
// adds a tab of this ip.
add('123.321.231.123');
// removes the ip.
remove('555.222.111.555');
console.dir(a);
&#13;