使用javascript循环遍历对象内的数组

时间:2022-11-29 20:46:45

I have an object that contains 3 arrays, like this:

我有一个包含3个数组的对象,如下所示:

 {
  "year": [
    "1980",
    "1970"
  ],
  "month": [
    "01",
    "01"
  ],
  "day": [
    "1",
    "1"
  ]
}

I want to convert it to something like this:

我想将它转换为这样的东西:

['1980-01-1','1970-01-1']

How can I do this using underscore js or jQuery or pure js?

我怎么能用下划线js或jQuery或纯js做到这一点?

3 个解决方案

#1


2  

Loop over your object and fill your array with push

循环遍历对象并使用push填充数组

for(var i = 0; i < object.year.length; i++){
    myArray.push(object.year[i]+"-"+object.month[i]+"-"+object.day[i])
}

https://jsfiddle.net/t4q8t7ht/

#2


1  

This proposal uses Array#map() and an array for the keys.

该提议使用Array#map()和一个数组作为键。

var data = { "year": ["1980", "1970"], "month": ["01", "01"], "day": ["1", "1"] },
    result = function (data) {
        var i = 0,
            keys = ['year', 'month', 'day'],
            result = [];
        while (i < data[keys[0]].length) {
            result.push(keys.map(function (k) {
                return data[k][i];
            }).join('-'));
            i++;
        }
        return result;
    }(data);

document.write('<pre>' + JSON.stringify(result, 0, 4) + '</pre>');

#3


0  

You can give the year, month, and day arrays to lodash#zip that will transform them to array [[year, month, day], ...], and then Array#map that array with Array#join to construct an array of ['year-month-day', ...]:

您可以将年,月和日数组赋予lodash#zip,将它们转换为数组[[year,month,day],...],然后使用Array#join将数组映射到该数组以构造数组['年 - 月 - 日',...]:

_.zip(myArray.year, myArray.month, myArray.day).map(function(arr) {
  return arr.join('-');
});

This solution does not need any additional variables and is more of functional programming style.

此解决方案不需要任何其他变量,更多的是函数式编程风格。

#1


2  

Loop over your object and fill your array with push

循环遍历对象并使用push填充数组

for(var i = 0; i < object.year.length; i++){
    myArray.push(object.year[i]+"-"+object.month[i]+"-"+object.day[i])
}

https://jsfiddle.net/t4q8t7ht/

#2


1  

This proposal uses Array#map() and an array for the keys.

该提议使用Array#map()和一个数组作为键。

var data = { "year": ["1980", "1970"], "month": ["01", "01"], "day": ["1", "1"] },
    result = function (data) {
        var i = 0,
            keys = ['year', 'month', 'day'],
            result = [];
        while (i < data[keys[0]].length) {
            result.push(keys.map(function (k) {
                return data[k][i];
            }).join('-'));
            i++;
        }
        return result;
    }(data);

document.write('<pre>' + JSON.stringify(result, 0, 4) + '</pre>');

#3


0  

You can give the year, month, and day arrays to lodash#zip that will transform them to array [[year, month, day], ...], and then Array#map that array with Array#join to construct an array of ['year-month-day', ...]:

您可以将年,月和日数组赋予lodash#zip,将它们转换为数组[[year,month,day],...],然后使用Array#join将数组映射到该数组以构造数组['年 - 月 - 日',...]:

_.zip(myArray.year, myArray.month, myArray.day).map(function(arr) {
  return arr.join('-');
});

This solution does not need any additional variables and is more of functional programming style.

此解决方案不需要任何其他变量,更多的是函数式编程风格。