所以我有这种来自json的数组
data : [{
article_categories : {
id : "xxx",
name : "xxx"
},
article : "xxx",
publisher : "xxx"
}]
我想为这些数组创建另一个多维数组,我想保留字段名称(数组中的名称"article"
,"publisher"
等等),但是我有值不知道获得字段名称
我还想做一些条件,如果只通过检查这个数组将一些字段包含到我的新数组中
thead: [
{ key: "article"},
{ key: "article_categories.name"},
.....
]
所以我最后会有像这样的数组
newArray: [
{article:"xxx",publisher: "xxx",article_categories.name:"xxx"},
{article:"xxx",publisher: "xxx",article_categories.name:"xxx"}
....
]
怎么做?我试过了
thead.forEach(function(column){
data.forEach(function(key,value){
if(column.key == key){
newArray[key] = value
}
})
})
但它只是不起作用......
答案 0 :(得分:1)
如果你愿意使用lodash,那就太简单了。 Lodash使用.get()
方法有效地评估JSON表达式,因此您不必为根据对象评估表达式而烦恼。
.chain()
增加了锦上添花,使代码更容易阅读,但在幕后执行许多复杂的操作。
请尝试以下代码段:
var keys = [{
key: "article"
},
{
key: "article_categories.name"
}
];
var data = [{
article_categories: {
id: "xxx",
name: "xxx"
},
article: "xxx",
publisher: "xxx"
}];
var result = _.chain(data)
.map(function(item) {
var object = {};
keys.forEach(function(key) {
object[key.key] = _.get(item, key.key);
});
return object;
})
.value();
console.log(result);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.4/lodash.min.js"></script>
答案 1 :(得分:1)
我认为首先,你应该简化这个:
thead = [
{ key: "article"},
{ key: "article_categories.name"},
.....
]
这样:
thead = ["article", "article_categories.name"]
我在这里:
const data = [{
article_categories : {
id : "xxx",
name : "xxx"
},
article : "xxx",
publisher : "xxx"
}],
thead = ["article", "article_categories.name"]
const newArray = data.map( obj => {
let output = {}
thead.forEach(key => {
if(key.includes(".")){
let subkeys = key.split(".")
output[key] = obj[subkeys[0]][subkeys[1]]
} else {
output[key] = obj[key]
}
})
return output
})
console.log(newArray)
&#13;
答案 2 :(得分:1)
您可以使用flatten函数以点符号展平对象。
然后map
展平的对象每个item
和filter
仅允许key
和reduce
重建对象。< / p>
要回答原始问题,您可以使用Object.keys()
获取对象的keys
let data = [{
article_categories : {
id : "xxx",
name : "xxx"
},
article : "xxx",
publisher : "xxx"
},{
article_categories : {
id : "xxx2",
name : "xxx2"
},
article : "xxx2",
publisher : "xxx2"
}]
let thead = [
{ key: "article"},
{ key: "article_categories.name"},
];
let allowed_keys = thead.map(x=> x.key);
let flattened = data.map(item => flatten(item, '', ''));
// console.log(flattened);
let result = flattened.map(item => {
return Object.keys(item)
.filter(key => allowed_keys.includes(key))
.reduce((obj, key) => {
obj[key] = item[key];
return obj;
}, {})
});
console.log(result);
/**
* Recursively flattens a JSON object using dot notation.
*
* NOTE: input must be an object as described by JSON spec. Arbitrary
* JS objects (e.g. {a: () => 42}) may result in unexpected output.
* MOREOVER, it removes keys with empty objects/arrays as value (see
* examples bellow).
*
* @example
* // returns {a:1, 'b.0.c': 2, 'b.0.d.e': 3, 'b.1': 4}
* flatten({a: 1, b: [{c: 2, d: {e: 3}}, 4]})
* // returns {a:1, 'b.0.c': 2, 'b.0.d.e.0': true, 'b.0.d.e.1': false, 'b.0.d.e.2.f': 1}
* flatten({a: 1, b: [{c: 2, d: {e: [true, false, {f: 1}]}}]})
* // return {a: 1}
* flatten({a: 1, b: [], c: {}})
*
* @param obj item to be flattened
* @param {Array.string} [prefix=[]] chain of prefix joined with a dot and prepended to key
* @param {Object} [current={}] result of flatten during the recursion
*
* @see https://docs.mongodb.com/manual/core/document/#dot-notation
*/
function flatten (obj, prefix, current) {
prefix = prefix || []
current = current || {}
if (typeof (obj) === 'object' && obj !== null) {
Object.keys(obj).forEach(key => {
flatten(obj[key], prefix.concat(key), current)
})
} else {
current[prefix.join('.')] = obj
}
return current
}
&#13;