1

I have a json format and want to convert from left to right

Here are my script. I had tried but cannot get the correct results. Please give some advice, thanks and appreciate.

function groupBy() {
var list = [{
        "id": "009",
        "Nm": "Model 1",
        "pid": "adidas"
    },
    {
        "id": "007",
        "Nm": "Model 1",
        "pid": "adidas"
    },
    {
        "id": "006",
        "Nm": "Model 1",
        "pid": "adidas"
    },
    {
        "id": "pm1",
        "Nm": "Model 1",
        "pid": "puma"
    },
    {
        "id": "003",
        "Nm": "Model 1",
        "pid": "adidas"
    },
    {
        "id": "pm5",
        "Nm": "Model 1",
        "pid": "puma"
    },
    {
        "id": "aj1",
        "Nm": "Model 1",
        "pid": "nike"
    },
    {
        "id": "aj2",
        "Nm": "Model 1",
        "pid": "nike"
    }
];
var output = [];
for (var i = 0; i < list.length; i++) {
    if (list[i].pid != undefined) {
        output.push(list[i]);
    }
}
console.log(output);
}
groupBy();

3 Answers 3

5

One option is to reduce into an object indexed by pids, whose values are arrays. On each iteration, create the array at the appropriate property if it doesn't exist, and then push to that array:

var list = [
  {"id":"009","Nm":"Model 1","pid":"adidas"},
  {"id":"007","Nm":"Model 1","pid":"adidas"},
  {"id":"006","Nm":"Model 1","pid":"adidas"},
  {"id":"pm1","Nm":"Model 1","pid":"puma"},
  {"id":"003","Nm":"Model 1","pid":"adidas"},
  {"id":"pm5","Nm":"Model 1","pid":"puma"},
  {"id":"aj1","Nm":"Model 1","pid":"nike"},
  {"id":"aj2","Nm":"Model 1","pid":"nike"}
];
console.log(
  list.reduce((a, item) => {
    const { pid } = item;
    if (!a[pid]) a[pid] = [];
    a[pid].push(item);
    return a;
  }, {})
);

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

Comments

1

You're pretty close there. But [] is to initialize an array instead of an object in javascript. In JS, it's {}.

Following is one of many ways you can accomplish this.

function groupBy() {
    var list = [
        {"id":"009","Nm":"Model 1","pid":"adidas"},
        {"id":"007","Nm":"Model 1","pid":"adidas"},
        {"id":"006","Nm":"Model 1","pid":"adidas"},
        {"id":"pm1","Nm":"Model 1","pid":"puma"},
        {"id":"003","Nm":"Model 1","pid":"adidas"},
        {"id":"pm5","Nm":"Model 1","pid":"puma"},
        {"id":"aj1","Nm":"Model 1","pid":"nike"},
        {"id":"aj2","Nm":"Model 1","pid":"nike"}
    ];

    // Initialize output as an object
    var output = {};

    for (var i = 0; i < list.length; i++){
        // 'objectKey' is where you group the list item by its 'pid'
        var objectKey = list[i].pid;

        // If there's a 'pid' in the list item, but 'output' is not an array yet, then..
        if (objectKey && !output.hasOwnProperty(objectKey)){
            // Initialize output.group to be an array
            output[ objectKey ] = [];
        }

        // Then finally, store the list into output's group that we created above.
        output[ objectKey ].push( list[i] );
    }

    console.log(output);
}               

groupBy();

1 Comment

it's work! Thanks for your explanation, Appreciate it!
0

Use this method for your any group by

const groupBy = function(arr, prop) {
  return arr.reduce(function(groups, item) {
    const val = item[prop]
    groups[val] = groups[val] || []
    groups[val].push(item)
    return groups
  }, {})
}

const list = [
                {"id":"009","Nm":"Model 1","pid":"adidas"},
                {"id":"007","Nm":"Model 1","pid":"adidas"},
                {"id":"006","Nm":"Model 1","pid":"adidas"},
                {"id":"pm1","Nm":"Model 1","pid":"puma"},
                {"id":"003","Nm":"Model 1","pid":"adidas"},
                {"id":"pm5","Nm":"Model 1","pid":"puma"},
                {"id":"aj1","Nm":"Model 1","pid":"nike"},
                {"id":"aj2","Nm":"Model 1","pid":"nike"}
            ];

const groupOutput = groupBy(list, 'pid');

You pass your key as second argument into groupBy for group by.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.