我有一个像这样的对象
var Data= {
"firstname": {
"excelcolumnName": "firstName",
"dbcolumn": "one",
"testinfo": "basictest"
},
"labname": {
"excelcolumnName": "LabName",
"dbcolumn": "two",
"testinfo": "basictest"
},
"percentCbd": {
"excelcolumnName": "Percent CBD",
"dbcolumn": "three",
"testinfo": "Final Test"
}
"percentgeneral": {
"excelcolumnName": "Percent General",
"dbcolumn": "four",
"testinfo": "Final Test"
}
}
我想过滤并创建包含 testinfo的新对象:" basictest" 单个对象的输出应该是这样的。
{
"one":"basictest",
"two":"basictest"
}
任何人都可以帮助我。
答案 0 :(得分:1)
根据我的理解,您需要一个包含dbcolumn:testinfo mappings
的对象var Data2={};
_.map(Data,(obj)=>{
if(obj.testinfo==='basictest'){
var newObj={};
newObj[obj.dbcolumn]=obj.testinfo;
return _.assign(Data2,newObj);
}
});
console.log(Data2);
map函数将每个对象“映射”为其他对象。 assign合并这些键值对。 newObj [obj.dbcolumn]行将'dbcolumn:value'中的值转换为新对象的键。
答案 1 :(得分:0)
Var array = [];
for(var i=0;i<Object.keys(Data).length;i++){
if(data[i].testinfo === 'basictest'){
array.push(data[i]);
}
}
数组将包含testinfo = 'basictest'
答案 2 :(得分:0)
试试这个
const Data = {
"firstname": {
"excelcolumnName": "firstName",
"dbcolumn": "one",
"testinfo": "basictest"
},
"labname": {
"excelcolumnName": "LabName",
"dbcolumn": "two",
"testinfo": "basictest"
},
"percentCbd": {
"excelcolumnName": "Percent CBD",
"dbcolumn": "three",
"testinfo": "Final Test"
},
"percentgeneral": {
"excelcolumnName": "Percent General",
"dbcolumn": "four",
"testinfo": "Final Test"
}
}
const result = {} // contain result
for (var i in Data) {
const item = Data[i]
result[item.dbcolumn] = item.testinfo // add a new object item
}
console.log(result) // print {one: "basictest", two: "basictest", three: "Final Test", four: "Final Test"}