繁体   English   中英

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

[英]Map an array and return a new one based on the object keys

这是原始数组:

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_timetime1time2进行组织

预期结果:

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 };
});

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

 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; } 

我希望这可以帮助你

 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); 

函数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; } 

 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); 

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

希望对您有所帮助。

 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) 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM