Insert element inside array

I have a function

checkName(output) {

  output.filter((NewData) => {
    return this.props.elements.filter((OldData) => {
      if (NewData.key == OldData.key) { =,
          //there i need to add another element 
          // Need to add newData.number = OldData.number
      return NewData
  return output

and I call this function like:

const named = this.checkName(product.rows)

Now I need to add to my product’s array that I passed to checkName the value "OldData.Number" to "newData.Number" that is not defined in product (so I need to create this field)

For example:

Product before the checkName function

product.rows = []

Product after the checkName function

product.rows = ["value of", NewData.number="value of OldData.number"]

How can I obtain this result?

24 thoughts on “Insert element inside array”

  1. You can get keys and values of the old object.

    const keys = Object.keys(oldObject);
    const values = Object.values(oldObject);
    // or
    const [keys, values] = Object.entries(oldObject);

    After, you will create a loop with all keys of oldObject, and insert in newObject like a array.

    keys.forEach( (key, index) => newObject[key] = values[index]);
    // or
    for (const [key, value] of Object.entries(object1)) {
      newObject[key] = value
  2. Use map like this.

    return NewData) =>{
     this.props.elements.forEach((OldData) => {
     if (NewData.key == OldData.key) { =;
         NewData.number = OldData.number;
     return NewData;
     // return output;

Leave a Comment