I want to construct a new array based on two sets of arrays. My current attempt is:
我想基于两组数组构建一个新数组。我目前的尝试是:
const mylist = [
{
"city" : "aa",
"country" : "de"
},
{
"city" : "bb",
"country" : "us"
},
{
"city" : "cc",
"country" : "ca"
},
{
"city" : "dd",
"country" : "za"
},
{
"city" : "ee",
"country" : "fr"
},
{
"city" : "ff",
"country" : "gr"
}
]
const stats = [{name : 'original', count: 'one'}, {name: 'copy', count: 'two'}, {name: 'redundant', count: 'three'}];
let myFinalList = [];
let str = 'hello.';
mylist.forEach(function (place, nn) {
let suffix = place.country;
stats.forEach(function (k) {
let items = {};
items[k.name] = str + k.count + '.' + suffix;
items['city'] = place.city;
myFinalList.push(items);
});
}, this);
console.log('print out: ', myFinalList);
the expected result is:
预期的结果是:
[ { original: 'hello.one.de', copy: 'hello.two.de', redundant: 'hello.three.de', city: 'aa' },
{ original: 'hello.one.us', copy: 'hello.two.us', redundant: 'hello.three.us', city: 'bb' },
...
{ original: 'hello.one.gr', copy: 'hello.two.gr', redundant: 'hello.three.gr', city: 'ff' }]
could somebody help me achieve this goal? i am confused and can't get the right array structure.
有人可以帮我实现这个目标吗?我很困惑,无法获得正确的阵列结构。
2 个解决方案
#1
2
i think you are missing the right position to declare the variables:
我认为你错过了声明变量的正确位置:
mylist.forEach(function (place, nn) {
let suffix = place.country;
let items = {};
stats.forEach(function (k) {
items[k.name] = str + k.count + '.' + suffix;
});
items['city'] = place.city;
myFinalList.push(items);
}, this);
#2
1
Why not we try using .map() and .reduce() and Template literals ?
为什么我们不尝试使用.map()和.reduce()以及模板文字?
/* transform every place in mylist ...*/
var finalList = mylist.map(place => {
/* 1. create a new ITEM object with CITY property set with PLACE.CITY.
2. for each stat, create a new property in ITEM and set its value with string interpolation..
3. return ITEM as the transformed object...*/
return stats.reduce((item, stat) => {
item[stat.name] = `${str}.${stat.count}.${place.country}`;
return item;
}, { city: place.city });
});
var mylist = [
{ city: "aa", country: "de" },
{ city: "bb", country: "us" },
{ city: "cc", country: "ca" },
{ city: "dd", country: "za" },
{ city: "ee", country: "fr" },
{ city: "ff", country: "gr" }
];
var stats = [
{ name: 'original', count: 'one' },
{ name: 'copy', count: 'two' },
{ name: 'redundant', count: 'three' }
];
var str = 'hello';
/* transform every place in mylist ...*/
var finalList = mylist.map(place => {
/* 1. create a new ITEM object with CITY property set with PLACE.CITY.
2. for each stat, create a new property in ITEM and set its value with string interpolation..
3. return ITEM as the transformed object...*/
return stats.reduce((item, stat) => {
item[stat.name] = `${str}.${stat.count}.${place.country}`;
return item;
}, { city: place.city });
});
console.log('print out: ', finalList);
#1
2
i think you are missing the right position to declare the variables:
我认为你错过了声明变量的正确位置:
mylist.forEach(function (place, nn) {
let suffix = place.country;
let items = {};
stats.forEach(function (k) {
items[k.name] = str + k.count + '.' + suffix;
});
items['city'] = place.city;
myFinalList.push(items);
}, this);
#2
1
Why not we try using .map() and .reduce() and Template literals ?
为什么我们不尝试使用.map()和.reduce()以及模板文字?
/* transform every place in mylist ...*/
var finalList = mylist.map(place => {
/* 1. create a new ITEM object with CITY property set with PLACE.CITY.
2. for each stat, create a new property in ITEM and set its value with string interpolation..
3. return ITEM as the transformed object...*/
return stats.reduce((item, stat) => {
item[stat.name] = `${str}.${stat.count}.${place.country}`;
return item;
}, { city: place.city });
});
var mylist = [
{ city: "aa", country: "de" },
{ city: "bb", country: "us" },
{ city: "cc", country: "ca" },
{ city: "dd", country: "za" },
{ city: "ee", country: "fr" },
{ city: "ff", country: "gr" }
];
var stats = [
{ name: 'original', count: 'one' },
{ name: 'copy', count: 'two' },
{ name: 'redundant', count: 'three' }
];
var str = 'hello';
/* transform every place in mylist ...*/
var finalList = mylist.map(place => {
/* 1. create a new ITEM object with CITY property set with PLACE.CITY.
2. for each stat, create a new property in ITEM and set its value with string interpolation..
3. return ITEM as the transformed object...*/
return stats.reduce((item, stat) => {
item[stat.name] = `${str}.${stat.count}.${place.country}`;
return item;
}, { city: place.city });
});
console.log('print out: ', finalList);