[英]Based on one array object keys and other array object values create one more new array?
[英]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_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 };
});
您可以使用一個數組作為鍵,並映射給定數組的映射值的結果。
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.