0

I have an array object and an object with the same amount of properties that array has.

I need to attach property values from object to every object of an array. To fill a value property

Array

0: {number: "1", type: "date", placeholder: "Semana", validations: Array(1), value: "value"}
1: {number: "2", type: "text", placeholder: "Marca", validations: Array(3), value: "value"}
[n]

Object

0: "23"
1: "34"
2: "sdfds"
[n]

Code

this.dataForm.forEach( (el) => {
    Object.entries(formObject).forEach(([key, value]) => {
      el.value = value
    })
})

Can you help with it?

Expected result

0: {number: "1", type: "date", placeholder: "Semana", validations: Array(1), value: 23}
1: {number: "2", type: "text", placeholder: "Marca", validations: Array(3), value: 34}

2 Answers 2

2

You can use map()

const arr = [{number: "1", type: "date", placeholder: "Semana", validations: Array(1), value: "value"},
{number: "2", type: "text", placeholder: "Marca", validations: Array(3), value: "value"}]

const obj = {
  0: "23",
  1: "34",
  2: "sdfds"
}

const res = arr.map((x,i) => ({...x,value:obj[i]}))
console.log(res)

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

2 Comments

@MikeKylmäLampi Consider accepting the answer if you are satisfied with answer.
have to wait 10 minutes at least
0

You can use forEach and access the actual index in each iteration to assing the proper value:

data = [
    {number: "1", type: "date", placeholder: "Semana", validations: Array(1), value: "value"},
    {number: "2", type: "text", placeholder: "Marca", validations: Array(3), value: "value"}
  ]
values = ["23", "34"]

// Here you do the magic :)
data.forEach((row, index) => row.value = values[index])

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.