映射数组并根据对象键返回一个新数组

时间:2018-05-09 17:49:45

标签: javascript arrays

这是原始数组:

let list = [
    {
        city: "new york", 
        current_time: "123", 
        time1: "456", 
        time2: "789",
    },
    {
        city: "london",
        current_time: "123",
        time1: "456",
        time2: "789",
    },
    {
        city: "tokyo",
        current_time: "123",
        time1: "456",
        time2: "789",
    }
]

我试图创建一个数组数组,其中每个内部数组包含具有城市和时间的对象。但是由 current_time time1 time2 组织。

预期结果:

result = [
    [{
        city: "new york",
        time: "123"
    }, {
        city: "london",
        time: "123"
    }, {
        city: "tokyo",
        time: "123"
    }],
    [{
        city: "new york",
        time: "456"
    }, {
        city: "london",
        time: "456"
    }, {
        city: "tokyo",
        time: "456"
    }], [{
        city: "new york",
        time: "789"
    }, {
        city: "london",
        time: "789"
    }, {
        city: "tokyo",
        time: "789"
    }]
]

我尝试使用map功能,但我能够使用current_time创建数组,我可能需要迭代键但是如果我需要使用forEach或更好的话我会感到困惑迭代的方式。

result = list.map((element, index) => {
    if(element.current_time) {
        return { city: element.city, time: element.current_time };
});

5 个答案:

答案 0 :(得分:2)

您可以使用数组作为键,并映射给定数组的映射值的结果。



var list = [{ city: "new york", current_time: "123", time1: "456", time2: "789" }, { city: "london", current_time: "123", time1: "456", time2: "789" }, { city: "tokyo", current_time: "123", time1: "456", time2: "789" }],
    keys = ["current_time", "time1", "time2"],
    result = keys.map(k => list.map(o => ({ city: o.city, time: o[k] })));

console.log(result);

.as-console-wrapper { max-height: 100% !important; top: 0; }




另一种方法可能是迭代数据并通过迭代用于使用键索引来确定对象的键来减少数据。



var list = [{ city: "new york", current_time: "123", time1: "456", time2: "789" }, { city: "london", current_time: "123", time1: "456", time2: "789" }, { city: "tokyo", current_time: "123", time1: "456", time2: "789" }],
    keys = ["current_time", "time1", "time2"],
    result = list.reduce((r, o) => {
        keys.forEach((k, i) => (r[i] = r[i] || []).push({ city: o.city, time: o[k] }));
        return r;
    }, []);

console.log(result);

.as-console-wrapper { max-height: 100% !important; top: 0; }




稍微修改了一些缺少密钥的数据版本。



var list = [{ city: "new york", current_time: "123", time1: "456", time2: "789" }, { city: "london", time1: "456", time2: "789" }, { city: "tokyo", current_time: "123", time1: "456" }],
    keys = ["current_time", "time1", "time2"],
    result = list.reduce((r, o) => {
        keys.forEach((k, i) => k in o && (r[i] = r[i] || []).push({ city: o.city, time: o[k] }));
        return r;
    }, []);

console.log(result);

.as-console-wrapper { max-height: 100% !important; top: 0; }




答案 1 :(得分:0)

我希望这有助于你

let list = [{
  city: "new york",
  current_time: "123",
  time1: "456",
  time2: "789"
}, {
  city: "london",
  current_time: "123",
  time1: "456",
  time2: "789"
}, {
  city: "tokyo",
  current_time: "123",
  time1: "456",
  time2: "789"
}, {
  city: "tokyo",
  current_time: "1223",
  time1: "456",
  time2: "789"
}, {
  city: "tokyo",
  current_time: "1223",
  time1: "456",
  time2: "789"
}],
obj = {};

list.forEach(function(itm) {
  if (!obj.hasOwnProperty(itm.current_time)) {
    obj[itm.current_time] = [];
  }
  obj[itm.current_time].push(itm);
});

let result = [];
var keys = Object.keys(obj);
for (var i = 0; i < keys.length; i++) {
  result.push(obj[keys[i]]);
}

console.log(result);

答案 2 :(得分:0)

函数map创建一个与原始数组长度相同的新数组,因此,您需要做的是循环该数组并对这些值进行分组。

另一种方法是使用函数reduce

let list = [{ city: "new york",   current_time: "123",   time1: "456",   time2: "789"}, { city: "london",   current_time: "123",   time1: "456",   time2: "789"},{ city: "tokyo",  current_time: "123",  time1: "456",  time2: "789"}],
    keys = ['current_time', 'time1', 'time2'],
    result = Object.values(list.reduce((a, c) => {
      keys.forEach(k => (a[c[k]] || (a[c[k]] = [])).push({ city: c.city, time: c[k] }));
      return a;
    }, {}));

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

答案 3 :(得分:0)

var list = [{
        city: "new york",
        current_time: "123",
        time1: "456",
        time2: "789",
    },
    {
        city: "london",
        current_time: "123",
        time1: "456",
        time2: "789",
    },
    {
        city: "tokyo",
        current_time: "123",
        time1: "456",
        time2: "789",
    }
];
let timeKeys = Object.keys(list[0]);
timeKeys.shift();
let finalArray = timeKeys.map(val => list.map(nestedVal => ({
    city: nestedVal.city,
    time: nestedVal[val]
})));

console.log(finalArray);

答案 4 :(得分:0)

我采用这种方法定义了一个新的空数组,并为每个元素重置为空。通过遍历不是城市的每个元素属性的循环并使用属性city和time推送新对象来填充此数组。

希望它会有所帮助。

&#13;
&#13;
var list = [
{ city: "new york", 
  current_time: "123", 
  time1: "456", 
  time2: "789",
},
 { city: "london",
   current_time: "123",
   time1: "456",
   time2: "789",
},
{ city: "tokyo",
  current_time: "123",
  time1: "456",
  time2: "789",
}];
var arr = []
var result = list.map((ele) => {
   arr = []
   for(var prop in ele){
      if(prop != 'city') arr.push({city: ele['city'], time: ele[prop]})
     }
    return arr
  })
  
console.log('result =', result)
&#13;
&#13;
&#13;

相关问题