# 如何循环遍历一个对象形成形成条件

2023-03-18 17:52:50

{

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?"

}

Question0 Operator0 Condition0 LogicalOperator0 Question1 Operator1 Condition1

How do you rate our services? < 5 && How likely are you to recommend our services to others? != 6

## 3 回答

GCT1015

TA贡献1601条经验 获得超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贡献1658条经验 获得超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贡献1659条经验 获得超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)

• 3 回答
• 0 关注
• 10 浏览

0/150