Javascript递增列表的列表

Javascript递增列表的列表,javascript,jquery,arrays,Javascript,Jquery,Arrays,如果我有这样一个列表: var a= [['1'[0]],['2'[0]],['3'[0]],['4'[0]]]; var lists = { '1': [0], '2': [0] }; function incrementList(listKey, value) { if (lists[listKey]) { lists[listKey] = lists[listKey].map(function(num) { return

如果我有这样一个列表:

var a=  [['1'[0]],['2'[0]],['3'[0]],['4'[0]]];
var lists = {
    '1': [0],
    '2': [0]
};

function incrementList(listKey, value) {
    if (lists[listKey]) {
        lists[listKey] = lists[listKey].map(function(num) {
            return num + value;
        });
    }        
}

incrementList('1', 2);
console.log(lists['1'][0]); // 2

incrementList('1', 4);
console.log(lists['1'][0]); // 6
如果条件是另一个变量应等于'1','2','3','4',则增加其列表中的值,例如:

for(var i=0; i<a; i++{
 if(a[i] == z) {
   a[i] += z;
    }
}

for(var i=0;i假设数组的实际格式为:

// Note the commas between the inner elements which you don't have above.
var a = [['1',[0]],['2',[0]],['3',[0]],['4',[0]]];
因此,您就快到了,需要访问
a[i][1][0]
,并增加它
++
,而不是
+=z

示例:

console.log(a[1][1][0]);
// 0  (at element '2')

// z is the string value you are searching for...
for(var i=0; i < a.length; i++) {
  // Match the z search at a[i][0]
  // because a[i] is the current outer array and [0] is its first element
  if(a[i][0] == z) {
    // Increment the inner array value which is index [i][1][0]
    // Because the 1-element inner array is at [i][1] and its first element is [0]
    a[i][1][0]++;
  }
}
然后
z='4'

// This time the '4' element gets incremented
a.toString();
// "1,0,2,1,3,0,4,1"
//-------^^^-----^^^

更新:这是更新值所在的位置:


数组[1]0:1
z='4'
的递增值,我不确定您想要实现什么,但我认为它是这样的:

var a=  [['1'[0]],['2'[0]],['3'[0]],['4'[0]]];
var lists = {
    '1': [0],
    '2': [0]
};

function incrementList(listKey, value) {
    if (lists[listKey]) {
        lists[listKey] = lists[listKey].map(function(num) {
            return num + value;
        });
    }        
}

incrementList('1', 2);
console.log(lists['1'][0]); // 2

incrementList('1', 4);
console.log(lists['1'][0]); // 6

应该检查哪些内容,哪些内容应该递增?您当前的数组无效。根据字符串“1”“2”“3”“4”进行检查,相关的[0]值应该递增。谢谢,我们假设您的数组看起来确实像
var a=[['1',[0]],['2',[0]],['3',[0]],['4',[0]];
?嘿…
'1'[0]=='1'
'hello'[0]='h'
感谢我得到的回复:[Array[2],Array[2],Array[2],Array[2]]这些数组看起来不像是递增的。这可能是因为值是一个整数值而不是字符串吗?@user1869421是的,当然-你需要展开它们来检查它们的内部值。这就是我调用toString的原因()使它们在上面可见。[数组[2]0:“1”1:数组[1]长度:2原型:数组[0],数组[2]0:“2”1:数组[1]长度:2原型:数组[0],数组[2]0:“3”1:数组[1]长度:2原型:数组[0],数组[2]0:“4”1:数组[1]长度:2原型:数组[0]]@user1869421查看上图。这是我的JSFIDLE示例运行后的控制台输出。@user1869421如果不能一次访问它们,则需要循环。您可以将它们全部分配给单个数组,如
var b=[];for(var i=0;i