0

I would like to add a new object.key to my JSON array.

To do that I have defined a new variable var result = {"bad":1} then I'm pushing the items like this skippedData.push(item.metadata["pico:record"]["dc:description"],{result});

As JSON output I'm having:

[
  {
    "skippedData": [
      {
        "_": "Lungo le mura, al posto del baluardo nord-est, la cosiddetta Fortezza medicea è un degradato complesso di corpi di fabbrica eretti nella 2ª metà del '500 attorno al trecentesco cassero senese.",
        "$": {
          "xml:lang": "it"
        }
      },
      {
        "result": {
          "bad": 1
        }
      }
    ]
  }
]

Which is wrong since I'm having a new "result" object instead of a "result" object key.

My expected output should be:


[
  {
    "skippedData": [
      {
        "_": "Lungo le mura, al posto del baluardo nord-est, la cosiddetta Fortezza medicea è un degradato complesso di corpi di fabbrica eretti nella 2ª metà del '500 attorno al trecentesco cassero senese.",
        "$": {
          "xml:lang": "it"
        },
        "result": {
          "bad": 1
        }
      }
    ]
  }
]

What I'm doing wrong?

Regards

1 Answer 1

1

skippedData is an array. You are using push method which is adding elements to array.

As I understood u want to add data from two sources in one object, so u can use this way to build new object there with spread:

skippedData.push({ 
  ...item.metadata["pico:record"]["dc:description"],
  ...{result}
});

or equal with Object.assign

skippedData.push(Object.assign(
  {},
  item.metadata["pico:record"]["dc:description"],
  {result}
));

If u want exactly to modify object already existing in array, u can refer to it by key, e.g.

skippedData[0].result = result
Sign up to request clarification or add additional context in comments.

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.