DONT NOW
DONT NOW

Reputation: 7

how to loop in Two Object in javascript?

I have two objects like this

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

and now I want loop in the objects for this like:

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

i can do it?? how

Upvotes: 0

Views: 166

Answers (3)

Afshin Rahmati
Afshin Rahmati

Reputation: 133

just you can use a array and a object

let Object1 = { pending: '500', answer: '200', reject: '400' }
let Object2 =['pending', 'answer',  'reject']

let array =[]

for (let i = 0; i<Object2.length; i++) {
  array.push({status: Object1[Object2[i]], title:Object2[i]})
}

console.log(array)

Upvotes: 0

derpirscher
derpirscher

Reputation: 17382

You don't seem to need b for that at all

The simplest would probably be

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

Upvotes: 0

Lucas Bodin
Lucas Bodin

Reputation: 336

I think b must be a tab like this:

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

And we can have c with this code:

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)

Upvotes: 1

Related Questions