0

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

3
  • 2
    How is object b being used here? Can't you obtain your output using just a ? Commented Jul 27, 2021 at 8:33
  • Object.entries(a).map( ([status, title]) => ({status, title}) ) Commented Jul 27, 2021 at 8:52
  • @AvinashThakur you switched status and title ... The result of your mapping is [{status: "pending", title: "500"}, ...] futhermore the statuscode is a string instead of a number ... Commented Jul 27, 2021 at 9:04

3 Answers 3

1

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)

Sign up to request clarification or add additional context in comments.

Comments

0

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)

Comments

0

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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.