3 回答
TA贡献1827条经验 获得超4个赞
您可以为属性和数字部分采用嵌套循环,并将所有部分收集在一个数组中。
let data = { Condition0: "5", Condition1: "6", LogicalOperator0: "&&", Operator0: "<", Operator1: "!=", Question0: "How do you rate our services?", Question1: "How likely are you to recommend our services to others?" },
keys = ['Question', 'Operator', 'Condition', 'LogicalOperator'],
result = [],
i = 0;
outer: while (true) {
for (const part of keys) {
const key = `${part}${i}`;
if (!(key in data)) break outer;
result.push(data[key]);
}
i++;
}
console.log(result.join(' '));
TA贡献1890条经验 获得超9个赞
将对象分配给变量并从那里访问它:
var someName = {
Condition0: "5"
Condition1: "6"
LogicalOperator0: "&&"
Operator0: "<"
Operator1: "!="
Question0: "How do you rate our services?"
Question1: "How likely are you to recommend our services to others?"
}
//Accessing the values would look like:
//someName.question0 + somename.operator0 + somename.condition0...
如果您遍历该对象,则只能按照创建它的顺序访问它。您似乎需要以不同的顺序访问它。
TA贡献1895条经验 获得超3个赞
您可以将显示属性的顺序存储到一个数组中并操作该数组
const obj = {
Condition0: '5',
Condition1: '6',
LogicalOperator0: '&&',
Operator0: '<',
Operator1: '!=',
Question0: 'How do you rate our services?',
Question1: 'How likely are you to recommend our services to others?'
}
const order = [
'Question0',
'Operator0',
'Condition0',
'LogicalOperator0',
'Question1',
'Operator1',
'Condition1'
]
const res = order.map(prop => obj[prop]).join(' ')
console.log(res)
添加回答
举报