如何在javascript中循环两个对象?

jexiocij  于 2021-09-29  发布在  Java
关注(0)|答案(2)|浏览(268)

我有两个这样的物体

let a = { pending: '500', answer: '200', reject: '400' }
let b ={ Pending: 'pending', Answer: 'answer', Reject: 'reject' }

现在我想在对象中循环如下:

const C = [{status:200,title:"answer"},{status:400,title:"reject"}, {status:500,title:"pendeing"}]

我能做到??怎样

byqmnocz

byqmnocz1#

我想b一定是这样的标签:

let b = ['pending', 'answer', 'reject']

我们可以用c和这个代码:

var a = { pending: '500', answer: '200', reject: '400' }
var b =['pending', 'answer',  'reject']

let c =[]

for (let i = 0; i<b.length; i++) {
  c.push({status: a[b[i]], title:b[i]})
}

console.log(c)
brqmpdu1

brqmpdu12#

你似乎不需要 b 为了这个
最简单的可能是

var a = { pending: '500', answer: '200', reject: '400' }
var c = Object.entries(a).map(([title, status]) => ({status: +status, title}))
console.log(c);

相关问题