1

My object json is like this :

var a = [{
    "attributes": {
        "Code": "SGL",
        "Total": "811400"
    },
    "DayPrice": {
        "Date": "2016-07-22",
        "Rate": "811400"
    }
}];

I want change DayPrice to array like this :

var a = [{
    "attributes": {
        "Code": "SGL",
        "Total": "811400"
    },
    "DayPrice": [{
        "Date": "2016-07-22",
        "Rate": "811400"
    }]
}];

Any solution to solve my problem?

Thank you very much

0

3 Answers 3

2

You would need to loop through the array of objects and wrap the DayPrice property of each in an array, like this:

for (var i = 0; i < a.length; i++) {
    a[i].DayPrice = [a[i].DayPrice];
}

Working example

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

1 Comment

I need you help. Look here : stackoverflow.com/questions/39652796/…
2

Assign the an array with the property to the property.

a[1].DayPrice = [a[1].DayPrice];

Or use a loop:

var a = [{ "attributes": { "Code": "SGL", "Total": "811400" }, "DayPrice": { "Date": "2016-07-22", "Rate": "811400" } }];

a.forEach(function (a) {
    if ('DayPrice' in a) {
        a.DayPrice = [a.DayPrice];
    }
});

document.write('<pre>' + JSON.stringify(a, 0, 4) + '</pre>');

1 Comment

I need you help. Look here : stackoverflow.com/questions/39652796/…
0

Hope this will help

var a = [{
            "attributes": {
                "Code": "SGL",
                "Total": "811400"
            },
            "DayPrice": {
                "Date": "2016-07-22",
                "Rate": "811400"
            }
        }];

var _getDayPrice = a[0].DayPrice;

var _dayPriceArray = [];
_dayPriceArray.push({
  "Date":_getDayPrice.Date,
  "Rate":_getDayPrice.Rate
})
a[0].DayPrice=_dayPriceArray;
console.log(a);

Check this jsFiddle

1 Comment

I need you help. Look here : stackoverflow.com/questions/39652796/…

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.