How to remove and edit values ​​in a JSON array using jquery

I have a json array this way

var simple = [];

for (var i = 0; i < 5; i++) {
   simple.push({ id: id, name: name, mobile: mobile });
}

And the values ​​from this json array look like this:

[{id:1
  name:"Test"
  mobile:100},
{id:2
 name:"Test"
 mobile:200},
{id:3
 name:"Temp"
 mobile:300},
{id:4
 name:"Test"
 mobile:400},
{id:5
 name:"Temp"
 mobile:500}]

What I need to do is compare the entries in the json array based on the "name" key.

When comparing, if record1.name = record2.name, then I need to delete the entire record1 and add the "mobile" value of record1 with record2, this way.

This is the expected end result.

[{id:1
  name:"Test"
  mobile:100,200,400},
{id:2
 name:"Temp"
 mobile:300,500}]

I tried to remove this path. But unable to add "mobile" key values ​​to each other.

var removeItem = name;
alert('Array before removing the element = ' + simple);
simple = jQuery.grep(simple, 
                     function(value) { return value != removeItem; });
alert('Array before removing the element = ' + simple);

Can someone help me with this?

thank

Edit ======

I tried to answer No. 1 = cited Ismail Kuruka below,

Works great with existing IE keys if new bottom entry keys are added,

var input = 
        [{
          id:1,
          name:"Test",
          ssn:1,
          mobile:100,
          address:"A"
        },
        {
         id:2,
         name:"Test1",
         ssn:2,
         mobile:200,
         address:"B"
        },
        {
         id:3,
         name:"Temp",
         ssn:3,
         mobile:300,
         address:"C"
        },
        {
         id:4,
         name:"Test2",
         ssn:4,
         mobile:400,
         address:"D"
        },
        {
         id:5,
         name:"Temp1",
         ssn:5,
         mobile:500,
         address:"E"
        }]; 

"" , , "ssn" 1, "name": "test"

[{"id":1,"name":"Test","ssn":"Test","mobile":"100"},{"id":2,"name":"Test1","ssn":"Test1","mobile":"200"},{"id":3,"name":"Temp","ssn":"Temp","mobile":"300"},{"id":4,"name":"Test2","ssn":"Test2","mobile":"400"},{"id":5,"name":"Temp1","ssn":"Temp1","mobile":"500"}]

,

//This part transforms your input to a map for each "name" attribute
        //Each key has a value of array of "mobile" 
        var intermediateObject = {};
        for(var i = 0; i < input.length; i++) { 
          if(typeof intermediateObject[input[i].name] == 'undefined') {
            intermediateObject[input[i].name] = [];
          }
          else if(typeof intermediateObject[input[i].ssn] == 'undefined') {
                intermediateObject[input[i].ssn] = [];
          }
          intermediateObject[input[i].name].push(input[i].mobile);
          intermediateObject[input[i].ssn].push(input[i].mobile); 
        }
        //Here the intermediate transformation is re-adjusted to look like your 
        //intended output format
        var outputObject = [];
        var index = 1;
        for(elem in intermediateObject ) {
          outputObject.push({
            id: index++,
            name: elem,
            ssn : elem,
            mobile: intermediateObject[elem].join(",")
          });
        }
        console.log(JSON.stringify(outputObject));

. - .

    Output should be,

    [{
      id:1,
      name:"Test",
      ssn:1,
      mobile:100,
      address:"A"
    },
    {
     id:2,
     name:"Test1",
     ssn:2,
     mobile:200,
     address:"B"
    },
    {
     id:3,
     name:"Temp",
     ssn:3,
     mobile:300,
     address:"C"
    },
    {
     id:4,
     name:"Test2",
     ssn:4,
     mobile:400,
     address:"D"
    },
    {
     id:5,
     name:"Temp1",
     ssn:5,
     mobile:500,
     address:"E"
    }];

?

+4
2

.

 var input = 
[{
      id:1,
      name:"Test",
      ssn:1,
      mobile:100,
      address:"A"
    },
    {
     id:2,
     name:"Test",
     ssn:1,
     mobile:200,
     address:"A"
    },
    {
     id:3,
     name:"Temp",
     ssn:3,
     mobile:300,
     address:"C"
    },
    {
     id:4,
     name:"Test2",
     ssn:4,
     mobile:400,
     address:"D"
    },
    {
     id:5,
     name:"Temp1",
     ssn:5,
     mobile:500,
     address:"E"
    }]; 

//This part transforms your input to a map for each "name" attribute
//Each key has a value of array of "mobile" 
var intermediateObject = {};
for(var i = 0; i < input.length; i++) { 
  if(typeof intermediateObject[input[i].name] == 'undefined') {
    intermediateObject[input[i].name] = { ssn: null, address: null, content:[]};
  }
  intermediateObject[input[i].name].content.push(input[i].mobile); 
  intermediateObject[input[i].name].ssn = input[i].ssn; 
  intermediateObject[input[i].name].address= input[i].address; 
}


//Here the intermediate transformation is re-adjusted to look like your 
//intended output format
var outputObject = [];
var index = 1;

for(elem in intermediateObject ) {
  outputObject.push({
    id: index++,
    name: elem,
    ssn: intermediateObject[elem].ssn,
    address: intermediateObject[elem].address,
    mobile: intermediateObject[elem].content.join(",")
  });
}

: : : [{ "ID": 1, "" : "", "" : 1, "" : "", "": "100200" }, { "" : 2, "": "Temp", "" : 3, "" : "", "": "300" }, { "" : 3, "" : "Test2", "" : 4, "": "D" "": "400" }, { "" : 4, "" : "Temp1", " ": 5, "" : "", "": "500" }]

0

, , . , .

function final(arr) {
    var aux = {}, index = 0;
    return arr.reduce(function (memo, el) {
        var key = el.name,
            obj = aux[key];
        if (!obj) {
            aux[key] = {
                id: index++,
                name: key,
                mobile: el.mobile
            };
            memo = memo.concat(aux[key]);
        } else {
            obj.mobile += "," + el.mobile;
        }
        return memo;
    }, []);
}

console.log(final(arr));

fiddle.

, .

0

Source: https://habr.com/ru/post/1584334/


All Articles