嗨,我的项目中有以下代码。问题是最后一个项目被推入数组而不是每个项目。
JS函数
function getArrayObjForKeys(keys, source, desiredKeys) {
let arrayObj = [],
obj = {};
source.forEach(function(val, ind) {
keys.forEach(function(v, i) {
(function(v) {
if (val.hasOwnProperty(v)) {
let desKey = desiredKeys[i];
obj[desKey] = val[v];
}
}).call(this, v);
});
arrayObj.push(obj);
});
return arrayObj;
}
// Invoking function as below
// **************************
var source = [{
'country': 'USA',
'descr': 'United States'
}, {
'country': 'UK',
'descr': 'United Kingdom'
}];
var countryList = getArrayObjForKeys(['country', 'descr'], source, ['value', 'label']);
console.info(countryList);
所需的输出
[{'value':'USA','label':'United States'},{'value':'UK','label':'United Kingdom'}]
柱塞
https://next.plnkr.co/edit/3SKbaJo9r5QX8hex?open=lib%2Fscript.js
答案 0 :(得分:2)
您的问题是您要在内存中引用相同的obj
,从而修改两个对象。对此的快速解决方案是将对象声明移到foreach
循环中,因此您对每个对象都有自己的唯一引用(不相同)。
请参见下面的工作示例:
function getArrayObjForKeys(keys, source, desiredKeys) {
let arrayObj = []
source.forEach(function(val, ind) {
let obj = {}; /* Move into foreach */
keys.forEach(function(v, i) {
(function(v) {
if (val.hasOwnProperty(v)) {
let desKey = desiredKeys[i];
obj[desKey] = val[v];
}
}).call(this, v);
});
arrayObj.push(obj);
});
return arrayObj;
}
var source = [{
'country': 'USA',
'descr': 'United States'
}, {
'country': 'UK',
'descr': 'United Kingdom'
}];
var countryList = getArrayObjForKeys(['country', 'descr'], source, ['value', 'label']);
console.info(countryList);
答案 1 :(得分:1)
您可以使用reduce将源映射到所需的输出,以将键,desirableKeys和对象减少到新对象:
function getArrayObjForKeys(keys, source, desiredKeys) {
return source.map(function(obj) {
return keys.reduce(function(result, key, index) {
result[desiredKeys[index]] = obj[key];
return result;
}, {});
});
}
var source = [{ country: 'USA', descr: 'United States' }, { country: 'UK', descr: 'United Kingdom' }];
var countryList = getArrayObjForKeys(['country', 'descr'], source, ['value', 'label']);
console.info(countryList);
答案 2 :(得分:0)
这是使用reduce
,Object.entries
和map
的另一种方法:
function getArrayObjForKeys(keys, source, desiredKeys) {
return source.map(obj => Object.entries(obj).reduce((accum, [key, val]) => {
const keyIdx = keys.indexOf(key);
accum[desiredKeys[keyIdx]] = val;
return accum;
}, {}));
}
const source = [
{ 'country': 'USA', 'descr': 'United States' },
{ 'country': 'UK', 'descr': 'United Kingdom' }
];
const countryList = getArrayObjForKeys(['country', 'descr'], source, ['value', 'label']);
console.info(countryList);
答案 3 :(得分:0)
使用ES6(并且两个输入数组始终具有相同的长度)的更简洁的选择是:
const getArrayObjForKeys = (keys, source, desiredKeys) => source.map( obj => {
let output = {};
keys.forEach( (key,i) => output[desiredKeys[i]] = obj[keys[i]]);
return output;
})
const source = [{'country':'USA','descr':'United States'},{'country':'UK','descr':'United Kingdom'}],
countryList = getArrayObjForKeys(['country','descr'], source, ['value','label']);
console.info(countryList);
答案 4 :(得分:0)
getArrayObjForKeys(keys,source,desiredKeys){
let arrayObj=[];
source.forEach(function(val,ind){
let obj={};
keys.forEach(function(v,i){
(function(v){
if(val.hasOwnProperty(v)){
let desKey = desiredKeys[i];
obj[desKey] = val[v];
}
}).call(this, v);
});
arrayObj.push(obj);
});
return arrayObj;
}
在foreach循环中更新obj变量。
问题与obj变量
答案 5 :(得分:0)
如果您想要更少的代码,这是一个更具体的功能。
function easy(obj) {
return obj = obj.map(obj => {
return {"value": obj.country, "lable": obj.descr};
});
}
obj = [{
"country": "USA",
"descr": "United States"
}, {
"country": "UK",
"descr": "United Kingdom"
}];
obj = easy(obj);
console.log(obj);