1

I have json like

SampJson= [
    {
        data: "50",
        cIndex: "column0",rowno:"1"
    },
    {
        data: "5",
        cIndex: "column1",rowno:"1"
    },
    {
        data: "80",
        cIndex: "column0",rowno:"2"
    },
    {
        data: "65",
        cIndex: "column1",rowno:"2"
    }

];

I need to create a JSON like

[
    {
        rowno:1,
        column0:50,
        column1:5
    },
    {
        rowno:2,
        column0:80,
        column1:65
    }
];

Thanks in advance...

1
  • 2
    There is no JSON there. Just JavaScript objects / arrays / strings / numbers. Commented Aug 4, 2015 at 8:38

1 Answer 1

1

You should just do this:

sampJson = [
    {
        data: "50",
        cIndex: "column0",
        rowno: "1"
    },
    {
        data: "5",
        cIndex: "column1",
        rowno: "1"
    },
    {
        data: "80",
        cIndex: "column0",
        rowno: "2"
    },
    {
        data: "65",
        cIndex: "column1",
        rowno: "2"
    }
];

var results = [];

sampJson.forEach(function(el) {
    if (results[+el.rowno - 1] == null) {
        results[+el.rowno - 1] = new Object();
    }
    results[+el.rowno - 1].rowno = +el.rowno;
    results[+el.rowno - 1][el.cIndex] = +el.data;
});

console.log(results);

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.