0

Basically I have a json array and I want to create a new array in which I want to push all childNode and inside ChildNode its respective funding source name

var data = vardata={
  "costdata": [
    {
      fundingSource: 'A',
      childNode: [
        {
          childName: 'x',
          amt: 100
        }
      ]
    },
    {
      fundingSource: 'B',
      childNode: [
        {
          childName: 'y',
          amt: 200
        }
      ]
    }
  ]
}

Expected output is to create single json array and pushed funding source element inside respective childNode attributes

vardata={
  "costdata": [
    {
      childNode: [
        {
          fundingSource: 'A',
          childName: 'x',
          amt: 100
        }
      ]
    },
    {
      childNode: [
        {
          fundingSource: 'B',
          childName: 'y',
          amt: 200
        }
      ]
    }
  ]
}
2
  • JSON is a textual notation for data exchange. (More here.) If you're dealing with JavaScript source code, and not dealing with a string, you're not dealing with JSON. Commented Jan 5, 2022 at 11:25
  • Your best bet here is to do your research, search for related topics on SO and elsewhere, and give it a go. If you get stuck and can't get unstuck after doing more research and searching, post a minimal reproducible example showing your attempt and say specifically where you're stuck. People will be glad to help. Commented Jan 5, 2022 at 11:27

2 Answers 2

1
  var data = {
    "costdata": [
      {
        fundingSource: 'A',
        childNode: [
          {
            childName: 'x',
            amt: 100
          }
        ]
      },
      {
        fundingSource: 'B',
        childNode: [
          {
            childName: 'y',
            amt: 200
          }
        ]
      }
    ]
  }

let newData = [];
  for (let i = 0; i < data.costdata.length; i++) {
    const child = data.costdata[i];
    let childAry = [];
    if (child.childNode.length > 0) {
      childAry = [];
      for (let j = 0; j < child.childNode.length; j++) {
        const node = child.childNode[j];
        let dataObj = {
          fundingSource: child.fundingSource,
          childName: node.childName,
          amt: node.amt
        };
        childAry.push(dataObj);
      }
    }
    newData.push({ 'childNode': childAry });
  }
  console.log('newData==>', newData);

Here is a for loop to get your expected output

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

1 Comment

A good answer will always include an explanation why this would solve the issue, so that the OP and any future readers can learn from it.
0

If you just want to format object here is the solution:

var data = {
    "costdata": [
        {
            fundingSource: 'A',
            childNode: [
                {
                    childName: 'x',
                    amt: 100
                },
            ]
        },
        {
            fundingSource: 'B',
            childNode: [
                {
                    childName: 'y',
                    amt: 200
                }
            ]
        }
    ]
}

data["costdata"] = data["costdata"].map(el => {
    const fundingSource = el.fundingSource

    const updChildNode = el.childNode.map(child => {
        return {
            fundingSource,
            ...child
        }
    })
    return updChildNode
})

console.log(data)

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.