在多个数组中添加相同的值

Add same value in multiple arrays

我需要将相同的值压入数组数组。

values.map(function(){
   for (i=0; i<= values.length; i++){
      values[i].push('AdWords');
   } 
});

问题是只在 1 个数组中推送 2 次相同的值

[
 [
  "2018-06-06",
  "Services",
  "65",
  "1",
  "4690000",
  "4690000",
  "1.54%",
  "AdWords",
  "AdWords"
 ],
 [
  "2018-06-06",
  "Services",
  "65",
  "1",
  "4690000",
  "4690000",
  "1.54%"     
 ]
]

如果你想改变数组,你应该使用 forEach 而不是 map

let values = [
  ["2018-06-06","Services","65","1","4690000","4690000","1.54%",],
  ["2018-06-06","Services","65","1","4690000","4690000","1.54%"]
];

values.forEach(function(o) {
  o.push('AdWords');
});

console.log(values);

这对我有用

for (i=0; i< values.length; i++){
  values[i].push('AdWords');
} 

您可以使用简单的 for loop 进行此操作。

工作示例:

/* VALUES ARRAY */

var values = [
  [
  "2018-06-06",
  "Services",
  "65",
  "1",
  "4690000",
  "4690000",
  "1.54%"
 ],

 [
  "2018-06-06",
  "Services",
  "65",
  "1",
  "4690000",
  "4690000",
  "1.54%"     
 ]
];



/* OPERATION */

for (let i = 0; i < values.length; i++) {

    values[i].push('AdWords');
};



/* RESULT */

console.log(values);


N.B. 注意使用let声明循环迭代器变量i.

let allows you to declare variables that are limited in scope to the block, statement, or expression on which it is used.

来源: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/let

如果你可以使用 ES6,你可能会使用 for...of 语句

var values = [
  [
    "2018-06-06",
    "Services",
    "65",
    "1",
    "4690000",
    "4690000",
    "1.54%"
  ],
  [
    "2018-06-06",
    "Services",
    "65",
    "1",
    "4690000",
    "4690000",
    "1.54%"
  ]
];


for (let val of values) {
  val.push('AdWords');
};

console.log(values);