0

Considering the following two arrays. How can I append array "lines" to array "array1". I have tried .push but it appends outside of the array. I have also tried .unshift which doesn't give me the wanted result.

array1 = [
  {
    "Activity #": "1111111",
    "Customer": "Last, First",
    "Tenure": "0 Year 2 Months",
    "Account #": "0000000"
  }];

lines = [
  {
    "Line #": "1",
    "Action Required": "New",
    "Status": "Closed",
    "Product Line": "test line1",
    "Product": "product1"
  },
  {
    "Line #": "2",
    "Action Required": "New",
    "Status": "Closed",
    "Product Line": "test line2",
    "Product": "product2"
  }];

I would like something like this.

my_array = [
{
    "Activity #": "1111111",
    "Customer": "Last, First",
    "Tenure": "0 Year 2 Months",
    "Account #": "0000000",
    "lines": [{
            "Line #": "1",
            "fields": "keys"
        },
        {
            "Line #": "2",
            "fields": "keys"
        }]
}] 

With the mentioned used methods I get something like.

my_array = [
{
    "Activity #": "1111111",
    "Customer": "Last, First",
    "Tenure": "0 Year 2 Months",
    "Account #": "0000000"
}, [
    {
        "Line #": "1",
        "fields": "keys"
    }, {
        "Line #": "2",
        "fields": "keys"
    }]
];

Hope someone can help and my question is clear.

2
  • @Allan: As far as I can tell, this has nothing to do with JSON. Commented Mar 6, 2014 at 0:18
  • Yep I confirm, nothing to do with json, shorcut mistake while editing. Commented Mar 6, 2014 at 0:20

1 Answer 1

5

It looks like you want to add lines as property to object inside the array, so you'd have to do:

array1[0].lines = lines;

You haven't explained what exactly should happen if you have more elements in array1, so I cannot say anything about that.

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.