请帮助解决这个问题。我有一个看起来像这样的对象:
data: {
offersMonday: [
{
"name": "name1",
"description" "description 1"
},
{
"name": "name2",
"description": "description 2"
}
],
offersTuesday: [
{
"name": "name1",
"description" "description 1"
},
{
"name": "name2",
"description": "description 2"
}
],
offersWednesday: [
{
"name": "name1",
"description" "description 1"
},
{
"name": "name2",
"description": "description 2"
}
]
}
我需要在每个对象的实例下获取描述。做这个的最好方式是什么?
我应该使用filter()
功能吗?
感谢。
编辑:我需要将它们全部放在一个变量中。所以做offersMonday[0].description + offersTuesday[0].description
这样的事情并不是一件好事。
答案 0 :(得分:0)
您可以尝试这样的事情:
as
阅读具体说明。或类似以下内容获取所有描述:
data.offersMonday[0].description

答案 1 :(得分:0)
var descs=[];
for(key in data){
descs=descs.concat(data[key].map(el=>el.description));
}
console.log(descs);//array
console.log(descs.join(","));//string
遍历主Object,获取所有数组elems描述值,并将它们附加到descs数组。
答案 2 :(得分:0)
你可以尝试类似的东西:
SELECT (CASE WHEN (SELECT SubjectName FROM subjects) LIKE '%History%'
THEN (SELECT SubjectsID FROM Subjects)
ELSE intSubjectID
END)
答案 3 :(得分:0)
看起来你错过了一些:
标记来正确格式化,我已经重新格式化了以下输入数据:
const input = {
data: {
offersMonday: [{
name: "name1",
description: "description 1"
}, {
name: "name2",
description: "description 2"
}],
offersTuesday: [{
name: "name1",
description: "description 1"
}, {
name: "name2",
description: "description 2"
}],
offersWednesday: [{
name: "name1",
description: "description 1"
}, {
name: "name2",
description: "description 2"
}]
}
}
下面是一个移动对象并抓住你需要的功能。我删除了“”标记,因为它们不是有效的JS(我假设你从JSON获得了这个)。当JSON对象进来时,无论如何它都会为你删除引号。
这是适用于我的解决方案,如果格式不是您想要的,请告诉我。
function groupDescriptions(input) {
let aggregatedArray = [];
const data = input.data;
for (day in data) {
const innerArray = data[day];
innerArray.forEach((item) => {
aggregatedArray.push(item.description);
})
}
return aggregatedArray;
}
aggregatedArray
会返回此信息:
[ 'description 1',
'description 2',
'description 1',
'description 2',
'description 1',
'description 2' ]