从现有数据准备新数组时,它只追加最后一个元素JavaScript

时间:2015-07-31 11:00:37

标签: javascript arrays for-loop google-apps-script

我的数组看起来像这样:

var arr = [{name: 'Test', id: 1}, {name: 'Test', id: 2}, {name: 'Test', id: 3}];

当我循环遍历此数组时,我想准备新数组:

var new_arr = [];
for (var key in arr) {
    if (arr[key].name == 'Test') {
        new_arr.push(arr[key]);
    }
}

但当我Logger.log(new_arr)看起来时:

[{name: 'Test': id: 3}]

所以问题是:这段代码有什么问题!?当我记录每个项目都很好,但看起来它只推送最后一个元素。谢谢!

P.S。我在本地机器上测试了这个代码,一切正常!

5 个答案:

答案 0 :(得分:1)

It works perfectly in the Google Script editor too...

code :

function test(){
  var arr = [{name: 'Test', id: 1}, {name: 'Test', id: 2}, {name: 'Test', id: 3}];      
  var new_arr = [];
  for (var key in arr) {
    if (arr[key].name == 'Test') {
      new_arr.push(arr[key]);
    }
  }
  Logger.log(new_arr);
}

result :

enter image description here

答案 1 :(得分:0)

如果您使用的是Google Apps脚本的Logger.log()方法,则传递错误的参数,log()会输出一个字符串..

https://developers.google.com/apps-script/reference/base/logger#log

尝试这样做..然后再次检查:

CREATE TABLE #PROD
(
    PRODUCT VARCHAR(1),
    LOC VARCHAR(1),
    [YEAR] VARCHAR(4),
    [WEEK] INT,
    QUANTITY INT
)

INSERT INTO #PROD
SELECT 'A', 'x', 2015, 2, 30 union
SELECT 'A', 'x', 2015, 4, 34 union
SELECT 'A', 'x', 2015, 5, 45 union
SELECT 'A', 'x', 2015, 6, 56 union
SELECT 'A', 'x', 2015, 7, 324 union
SELECT 'A', 'x', 2015, 8, 56 union
SELECT 'A', 'x', 2015, 9, 67 union
SELECT 'A', 'x', 2016, 1, 34 union
SELECT 'A', 'x', 2016, 2, 76 union
SELECT 'A', 'x', 2016, 3, 45

DECLARE @WEEK INT
SET @WEEK = 1

DECLARE @DUMMY TABLE
(
    [WEEK] INT 
)

WHILE @WEEK <= 52
BEGIN
    INSERT INTO @DUMMY
    SELECT @WEEK
    SET @WEEK = @WEEK + 1
END

SELECT
    [ROW] = ROW_NUMBER() OVER(ORDER BY X.[YEAR], X.[WEEK]),
    X.*
FROM
(
SELECT
    PRODUCT = CASE WHEN P.PRODUCT IS NULL THEN 'A' ELSE P.PRODUCT END,
    LOC = CASE WHEN P.LOC IS NULL THEN 'x' ELSE P.LOC END,
    [YEAR] = CASE WHEN P.[YEAR] IS NULL THEN 2015 ELSE P.[YEAR] END,
    [WEEK] = D.[WEEK],
    QUANTITY = CASE WHEN P.QUANTITY IS NULL THEN 0 ELSE P.QUANTITY END
FROM
    @DUMMY AS D
LEFT JOIN
    #PROD AS P
ON
    D.[WEEK] = P.[WEEK]
AND P.[YEAR] = 2015

UNION

SELECT
    PRODUCT = CASE WHEN P.PRODUCT IS NULL THEN 'A' ELSE P.PRODUCT END,
    LOC = CASE WHEN P.LOC IS NULL THEN 'x' ELSE P.LOC END,
    [YEAR] = CASE WHEN P.[YEAR] IS NULL THEN 2016 ELSE P.[YEAR] END,
    [WEEK] = D.[WEEK],
    QUANTITY = CASE WHEN P.QUANTITY IS NULL THEN 0 ELSE P.QUANTITY END
FROM
    @DUMMY AS D
LEFT JOIN
    #PROD AS P
ON
    D.[WEEK] = P.[WEEK]
AND P.[YEAR] = 2016
) AS X

答案 2 :(得分:0)

我做的时候

console.debug(new_arr);

我得到了

[Object { name="Test",  id=1}, Object { name="Test",  id=2}, Object { name="Test",  id=3}]

所以你的代码很好。记录器似乎很时髦。

答案 3 :(得分:0)

我不明白为什么它不起作用。只要您在正确的位置使用Logger.log。我执行了以下操作,并在Logger.log循环后放置了for

function test() {
  var arr = [{
    name: 'Test',
    id: 1
  }, {
    name: 'Test',
    id: 2
  }, {
    name: 'Test',
    id: 3
  }];

  var new_arr = [];

  for (var key in arr) {
    if (arr[key].name == 'Test') {
      new_arr.push(arr[key]);
    }
  }

  Logger.log(new_arr);

}

当我查看日志时,结果就是您期望看到的结果:

  

[15-07-31 13:40:31:846 EDT] [{name = Test,id = 1.0},{name = Test,id = 2.0},{name = Test,id = 3.0}] < / p>

如果我误解了你的问题,请告诉我。

答案 4 :(得分:-1)

问题是你运行:

for(var key in arr)

用于循环对象属性。

使用以下内容循环数组

for (i = 0; i < arr.length; i++) { 
    if (arr[i].name == 'Test') {
        new_arr.push(arr[i]);
    }    
}

或者您可以使用Array.prototype.filter

var new_arr = arr.filter(function(item){
    return item.name === 'Test'
})

或者一个while循环,无论你喜欢什么