0

I need to transform below Json array to another array as mentioned below using angularjs/ javascript.

Input Array = [{"Name":"123", "Type": "Type1", "Total":"24"}, {"Name":"123", "Type": "Type2", "Total":"25"}, {"Name":"124", "Type": "Type1", "Total":"26"}, {"Name":"124", "Type": "Type2", "Total":"27"}]

Output Array: [{"Name":"123", "Type1":"24", "Type2":"25"}, {"Name":"124", "Type1":"26", "Type2":"27"}

3 Answers 3

1

I would work it out with reduce function I've added some comments for you inside as well:

let inputArray = [{"Name":"123", "Type": "Type1", "Total":"24"}, {"Name":"123", "Type": "Type2", "Total":"25"}, {"Name":"124", "Type": "Type1", "Total":"26"}, {"Name":"124", "Type": "Type2", "Total":"27"}];
// and do reduce function on it
inputArray.reduce((prevVal, currVal, index) => {

    // first check if there's Name in already
    if (prevVal.find(x => x.Name === currVal.Name)) {

        // if there's a Name same as current element use new type as a key and add Total
        prevVal.find(x => x.Name === currVal.Name)[currVal.Type] = currVal.Total;
    } else {

        // in case there's no Name then just create object with it
        prevVal.push({
            Name: currVal.Name,
            [currVal.Type]: currVal.Total
        });

        }

        return prevVal;
    }, []);

Here's fiddle: https://jsfiddle.net/pegla/s9hwbth4/

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

Comments

0

All you have to do is, get the unique objects from the original list based on Name.

var obj = [{
        "Name": "123",
        "Type": "Type1",
        "Total": "24"
    },
    {
        "Name": "123",
        "Type": "Type2",
        "Total": "25"
    },
    {
        "Name": "124",
        "Type": "Type1",
        "Total": "26"
    },
    {
        "Name": "124",
        "Type": "Type2",
        "Total": "27"
    }
];
var getIndex = function(list, property, object) {
    for (var i = 0; i < list.length; i++) {
        if (list[i][property] == object[property]) {
            return i;
        }
    }
    return -1;
}
var result = [];
for (var i = 0; i < obj.length; i++) {
/*  Call getIndex to return the index of element in the result and add the type name property */
    var index = getIndex(result, 'Name', obj[i]);
    if (index != -1) {
        result[index][obj[i].Type] = obj[i].Total;
    } else {
        var newObj = {};
        newObj.Name = obj[i].Name;
        newObj[obj[i].Type] = obj[i].Total;
        result.push(newObj);
    }
}
console.log(result);

2 Comments

Just giving a running demo is not a good practice, you should also explain your code using code snippets.
In progress Varun
0

You don't need angularjs to do that. You can do that using simple javascript for block. Please see attached code. Best,

let input = [{"Name":"123", "Type": "Type1", "Total":"24"}, {"Name":"123", "Type": "Type2", "Total":"25"}, {"Name":"124", "Type": "Type1", "Total":"26"}, {"Name":"124", "Type": "Type2", "Total":"27"}];

let output = [];

for(let i=0; i<input.length-1;i++){
    output.push({
        "Name":input[i].Name,
        "Type1":input[i].Total,
        "Type2":input[i+1].Total,
      });
      i++;
}

console.log(output);

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.