gpt4 book ai didi

javascript - 如何使用 jquery 删除和编辑 JSON 数组中的值

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:43:16 25 4
gpt4 key购买 nike

这样我就有了一个json数组,

var simple = [];

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

这个json数组的值如下,

[{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}]

我需要做的是,我必须根据“name”键比较 json 数组中的记录。

比较时,如果 record1.name = record2.name 那么我必须删除整个 record1 并将 record1 的“移动”值附加到 record2 中,这样。

这是预期的最终输出。

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

我试过用这种方式删除。但无法将“移动”键值相互附加。

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);

有人可以帮助我吗?

谢谢

编辑======

我试过下面给出的答案 #1=Ismail Kuruca 给出的,

它与现有 key I.E 一起工作正常,如果使用以下输入添加新 key ,

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"
}];

你能帮忙吗?

最佳答案

这是一个将输入转换为所需输出的非常粗略的示例。

 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,"name":"Test","ssn":1,"address":"A","mobile":"100,200"},{"id":2,"name": "Temp","ssn":3,"address":"C","mobile":"300"},{"id":3,"name":"Test2","ssn":4,"address ":"D","mobile":"400"},{"id":4,"name":"Temp1","ssn":5,"address":"E","mobile":"500 "}]

关于javascript - 如何使用 jquery 删除和编辑 JSON 数组中的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29830497/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com