我的要求非常简单:遍历数组,使用对象确定所选对象,然后将其推入新数组,但是我不知道如何编写?
想要的结果如下:
[{
key: 'name',
name: "A",
},
{
key: 'note',
name: "C”,
}
]
我的代码:
var data = [{
"data": {
"list": [{
"name": "A",
"key": "name",
}, {
"name": "B",
"key": "title",
}, {
"name": "C",
"key": "note",
}, {
"name": "D",
"key": "desc",
}],
"show": [
"title",
"desc"
]
}
}]
var arr = []
data[0].data.list.map(item => {
data[0].data.show.forEach(prop => {
if (prop !== item.key) {
arr.push({
key: item.key,
name: item.name
})
}
})
})
console.log(arr);
答案 0 :(得分:3)
您可以检查键是否包含在app.post('/upload/file', upload.single('file'), (req, res) => {
cloudconvert.createProcess(
{ inputformat: 'docx', outputformat: 'pdf' },
(err, process) => {
if (err) {
console.error(`CloudConvert Process creation failed: ${err}`)
} else {
process.start({
input: {
s3: {
accesskeyid: SPACES_ACCESS_KEY_ID,
secretaccesskey: SPACES_SECRET_ACCESS_KEY,
bucket: 'files'
}
},
file: req.file.key,
outputformat: 'pdf',
output: {
s3: {
accesskeyid: SPACES_ACCESS_KEY_ID,
secretaccesskey: SPACES_SECRET_ACCESS_KEY,
bucket: 'files'
}
}
}, (err, process) => {
if(err) return console.log(err.message)
console.log('process', process)
})
}
}
)
})
属性中,然后按下对象。
show
var data = [{ data: { list: [{ name: "A", key: "name" }, { name: "B", key: "title" }, { name: "C", key: "note" }, { name: "D", key: "desc" }], show: ["title", "desc"] } }],
arr = [];
data[0].data.list.map(({ key, name }) => {
if (!data[0].data.show.includes(key)) {
arr.push({ key, name });
}
});
console.log(arr);
答案 1 :(得分:1)
尝试
let d=data[0].data;
let arr= d.list.filter(x=> !d.show.includes(x.key));
var data = [{
"data": {
"list": [{
"name": "A",
"key": "name",
}, {
"name": "B",
"key": "title",
}, {
"name": "C",
"key": "note",
}, {
"name": "D",
"key": "desc",
}],
"show": [
"title",
"desc"
]
}
}]
let d=data[0].data;
let arr= d.list.filter(x=> !d.show.includes(x.key));
console.log(arr);
答案 2 :(得分:0)
var data = [{
"data": {
"list": [{
"name": "A",
"key": "name",
}, {
"name": "B",
"key": "title",
}, {
"name": "C",
"key": "note",
}, {
"name": "D",
"key": "desc",
}],
"show": [
"title",
"desc"
]
}
}]
var arr = []
data[0].data.list.forEach(keyValue=> !data[0].data.show.includes(keyValue.key) ? arr.push(keyValue): null)
console.log(arr);
答案 3 :(得分:0)
var data = [{
"data": {
"list": [{
"name": "A",
"key": "name",
}, {
"name": "B",
"key": "title",
}, {
"name": "C",
"key": "note",
}, {
"name": "D",
"key": "desc",
}],
"show": [
"title",
"desc"
]
}
}]
var arr = data[0].data.list.filter(item => data[0].data.show.every(props => props !== item.key))
console.log(arr);