在一个数组中有一个可选数组,它具有对象键作为字符串,我想返回新数组中的所有字符串。
this.arr = [
{
text: "some text",
numberValue: 560,
booleanValue: true,
arrayValue: [
{
text: "I am some text",
numberValue: 13,
booleanValue: false
}
]
},
{
text: "some",
numberValue: 0,
booleanValue: true,
arrayValue: [
{
text: "I some text",
numberValue: 12333,
booleanValue: true
}
]
},
{
text: "someddkjd",
numberValue: 0,
booleanValue: true
},
{
text: "someckckk",
numberValue: 0,
booleanValue: true,
arrayValue: [
{
text: "Ime text",
numberValue: 12333,
booleanValue: true
}
]
}
]
我想返回一个以arrayValue的string属性为目标的字符串数组 我想要的结果是
newArray = ["Iam some text", "I some text", "Ime text"];
我尝试的是
for(let i = 0; i < this.arr.length; i++){
this.stringArr.push(this.arr[i].arrayValue[i].text);
}
该问题的其他几点: arrayValue是可选的,可以存在或不存在,就像是否看到对象number3没有arrayValue一样。 另外,arrayValue内可以有多个对象
答案 0 :(得分:0)
做类似的事情
this.stringArr = [];
for(let item of this.arr){
for(let item2 of item.arrayValue){
this.stringArr.push(item2.text);
}
}
答案 1 :(得分:0)
假设arrayValue
数组中可以有许多个对象,那么您可以使用 Array.map
从中获取字符串。如果没有arrayValue
属性,我们可以通过 Array.filter
过滤掉未定义的值。
const arr = [{"text":"some text","numberValue":560,"booleanValue":true,"arrayValue":[{"text":"I am some text","numberValue":13,"booleanValue":false}]},{"text":"some","numberValue":0,"booleanValue":true,"arrayValue":[{"text":"I some text","numberValue":12333,"booleanValue":true}]},{"text":"someddkjd","numberValue":0,"booleanValue":true},{"text":"someckckk","numberValue":0,"booleanValue":true,"arrayValue":[{"text":"Ime text","numberValue":12333,"booleanValue":true}]}];
const newArray = arr.map(obj => obj.arrayValue && Array.isArray(obj.arrayValue) ? obj.arrayValue.map(ele => ele.text).join("") : undefined).filter(obj => obj);
console.log(newArray);
我们可以使用 Array.reduce
进行相同的操作,在逻辑中,我首先检查了属性是否存在,然后通过执行Array.isArray
来检查属性是否确实是数组。是,然后在joined
中对象的所有text
属性上arrayValue
:
const arr = [{"text":"some text","numberValue":560,"booleanValue":true,"arrayValue":[{"text":"I am some text","numberValue":13,"booleanValue":false}]},{"text":"some","numberValue":0,"booleanValue":true,"arrayValue":[{"text":"I some text","numberValue":12333,"booleanValue":true}]},{"text":"someddkjd","numberValue":0,"booleanValue":true},{"text":"someckckk","numberValue":0,"booleanValue":true,"arrayValue":[{"text":"Ime text","numberValue":12333,"booleanValue":true}]}];
const newArray = arr.reduce((acc, ele) => ele.arrayValue && Array.isArray(ele.arrayValue) ? acc.concat(ele.arrayValue.map(ele => ele.text).join("")) : acc, []);
console.log(newArray);
我已经使用 Array.join()
来连接内部arrayValue
对象的文本属性。
答案 2 :(得分:0)
const arr = [{text:"some text",numberValue:560,booleanValue:true,arrayValue:[{text:"I am some text",numberValue:13,booleanValue:false}]},{text:"some",numberValue:0,booleanValue:true,arrayValue:[{text:"I some text",numberValue:12333,booleanValue:true}]},{text:"someddkjd",numberValue:0,booleanValue:true},{text:"someckckk",numberValue:0,booleanValue:true,arrayValue:[{text:"Ime text",numberValue:12333,booleanValue:true}]}]
const texts = arr.reduce((r, { arrayValue }) =>
arrayValue ? r.concat(arrayValue.map(a => a.text)) : r
, [])
console.log(texts)