我的代码似乎返回未在对象中明确声明的空值。
我要编写的代码是将遍历对象数组的代码,找到哪个值为null,然后添加一个等于“嗨”的“ question”(问题)键,请问您能否提供“ key”。 “最后,我想用问号返回对象数组。
请参见下面的代码:
var list1 = [
{ firstName: null, lastName: 'I.', country: 'Argentina', continent: 'Americas', age: 35, language: 'Java' },
{ firstName: 'Lukas', lastName: 'X.', country: 'Croatia', continent: 'Europe', age: 35, language: null },
{ firstName: 'Madison', lastName: 'U.', country: 'United States', continent: 'Americas', age: 32, language: 'Ruby' }
];
function askForMissingDetails(list) {
for (let i = 0; i < list.length; i++) {
for (var prop in list[i]) {
if ( list[i].prop == null)
list[i]['question'] = `Hi, could you please provide your ${prop}.`;
else
list.splice(i,1);
}
}
return list;
}
console.log(askForMissingDetails(list1))
我得到的输出是:
[
0: {
firstName: null
lastName: "I."
country: "Argentina"
continent: "Americas"
age: 35
language: "Java"
question: "Hi, could you please provide your question."
}
1: {
firstName: "Lukas"
lastName: "X."
country: "Croatia"
continent: "Europe"
age: 35
language: null
question: "Hi, could you please provide your question."
}
2: {
firstName: "Madison"
lastName: "U."
country: "United States"
continent: "Americas"
age: 32
language: "Ruby"
question: "Hi, could you please provide your language."
}]
答案 0 :(得分:2)
似乎您在混淆访问对象的方式。首先,您正在阅读list[i].prop
。这将寻找一个实际上叫做prop
的属性。要解决此问题,请将其更改为list[i][prop]
。在这种情况下,方括号表示动态查找。您不是在寻找prop
键,而是在寻找prop
的值的键。
答案 1 :(得分:1)
如果您的对象中有多个null值,我会使用reduce来创建一个新的列表,并带有属性问题以保留在数组中:
var list1 = [
{
firstName: null,
lastName: "I.",
country: "Argentina",
continent: "Americas",
age: 35,
language: "Java"
},
{
firstName: "Lukas",
lastName: "X.",
country: "Croatia",
continent: "Europe",
age: 35,
language: null
},
{
firstName: "Madison",
lastName: "U.",
country: "United States",
continent: "Americas",
age: 32,
language: "Ruby"
},
{
firstName: "Christian",
lastName: null,
country: "Perú",
continent: null,
age: 32,
language: null
}
];
// using list1 as context to apply reduce to be saved on myNewList
const myNewList = list1.reduce((acc, item) => {
// itering keys names of the current object
Object.keys(item).forEach(key => {
// if the current key name his value is null
if (!item[key]) {
// if haven't a key name called question, i create it as a new empty array
if (!item.question) item.question = [];
// pushing into the array the message with the current key name(because is null)
item.question.push(`Hi, could you please provide your ${key}.`);
}
});
// pushing into the accumulator the current object
if (item.question) acc.push(item);
return acc;
}, []);
console.log(myNewList);
答案 2 :(得分:0)
let result = list1.reduce((acc, list) => {
let missedKey = "";
for(let key in list){
if(!list[key]) missedKey = key
}
if(missedKey) {
list.question = `Hi, could you please provide your ${missedKey}.`
acc.push(list)
};
return acc;
}, []);
答案 3 :(得分:0)
我相信您要执行的是 map / reduce 操作。
您想更改原始数组,以包含存在getCutPlanes
值(“映射” )的问题,并过滤出完整的条目(“ reduce” < / em>)。
这一切都可以使用Array.prototype.reduce()
viewer.getCutPlanes()
//[n.Vector4]0: n.Vector4 {x: 0.5455882464475988, y: 0, z: 0.8380533785733665, w: 2.9219428579920317}length: 1__proto__: Array(0)