1

I have an array that looks like this

[{
    "Inventory": {
        "dashboard_id": "Inventory",
        "filter_by": "Location",
        "yAxis": "Quantity",
        "title": "",
        "chart_type": "-------Select-------",
        "mainchart": "Yes"
    },
    "Quality": {
        "dashboard_id": "Quality",
        "filter_by": "Location",
        "yAxis": "SampleNo",
        "title": "",
        "chart_type": "-------Select-------",
        "mainchart": "Yes"
    }
}]

I need to add more values into each object. How do I ADD to an existing array so it might look something like this

{
    "Inventory": [{
        "dashboard_id": "Inventory",
        "filter_by": "Location",
        "yAxis": "Quantity",
        "title": "",
        "chart_type": "-------Select-------",
        "mainchart": "Yes"
    }, {
        "dashboard_id": "Inventory",
        "filter_by": "Location",
        "yAxis": "Quantity",
        "title": "",
        "chart_type": "-------Select-------",
        "mainchart": "Yes"
    }],
    "Quality": {
        "dashboard_id": "Quality",
        "filter_by": "Location",
        "yAxis": "SampleNo",
        "title": "",
        "chart_type": "-------Select-------",
        "mainchart": "Yes"
    }
}

I add into the array using nestedData[dashId] = data;

Where dashId consist of Quality, 'Inventory', etc.

data is

{
    "dashboard_id": "Inventory",
    "filter_by": "Location",
    "yAxis": "Quantity",
    "title": "",
    "chart_type": "-------Select-------",
    "mainchart": "Yes"
}

1 Answer 1

4

Build a new Object using the literal syntax, which contains the desired arrays and assigns the objects from the source array to the respective property in the object.

var arr = [{
    "Inventory": {
        "dashboard_id": "Inventory",
        "filter_by": "Location",
        "yAxis": "Quantity",
        "title": "",
        "chart_type": "-------Select-------",
        "mainchart": "Yes"
    },
    "Quality": {
        "dashboard_id": "Quality",
        "filter_by": "Location",
        "yAxis": "SampleNo",
        "title": "",
        "chart_type": "-------Select-------",
        "mainchart": "Yes"
    }
}];

var obj = {Inventory:[arr[0].Inventory], Quality:[arr[0].Quality]};
obj.Inventory.push({
    "dashboard_id": "Inventory",
    "filter_by": "Location",
    "yAxis": "Quantity",
    "title": "",
    "chart_type": "-------Select-------",
    "mainchart": "Yes"
});

console.log(obj);

Working Example: http://jsfiddle.net/vJjSX/

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.