Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/csharp-4.0/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript-从Javascriptmap中删除元素_Javascript_Arrays_Dictionary - Fatal编程技术网

Javascript-从Javascriptmap中删除元素

Javascript-从Javascriptmap中删除元素,javascript,arrays,dictionary,Javascript,Arrays,Dictionary,我有以下Javascript对象 [ "{\"id\":\"b00a3a47-783a-4af5-90d9-59c4deb7a9e3\",\"notes\":\"sdfsdf\",\"recordType\":0}", "{\"id\":\"a6f72972-502e-452b-9773-51699a527122\",\"notes\":\"sdfsfdf\",\"recordType\":0}" ] 如何移除ID=a6f72972-502e-452b-9773-51699a5

我有以下Javascript对象

[  
   "{\"id\":\"b00a3a47-783a-4af5-90d9-59c4deb7a9e3\",\"notes\":\"sdfsdf\",\"recordType\":0}",
   "{\"id\":\"a6f72972-502e-452b-9773-51699a527122\",\"notes\":\"sdfsfdf\",\"recordType\":0}"
]
如何移除ID=a6f72972-502e-452b-9773-51699a527122的元件? 不一定是a6f72972-502e-452b-9773-51699a527122,这只是一个例子

我试过以下方法

var index = detailsArray.map(function (element) {
                    console.log("element = " + JSON.stringify(element) + " index = " + index + " id = " + element.id);
                    return element.id;
                }).indexOf(detailId);
                console.log("index of " + detailId + " = " + index);
                delete detailsArray[index];

但它将返回未定义的element.id。我怀疑这是因为元素的“属性”是一个字符串,我不确定如何解决这个问题。

它只是一个JSON字符串数组。 如果要过滤它们,只需解析每个项目并检查id是否相等:

var arr = [  
   "{\"id\":\"b00a3a47-783a-4af5-90d9-59c4deb7a9e3\",\"notes\":\"sdfsdf\",\"recordType\":0}",
   "{\"id\":\"a6f72972-502e-452b-9773-51699a527122\",\"notes\":\"sdfsfdf\",\"recordType\":0}"
];

var result = arr.filter(function(x) {
  return JSON.parse(x).id !== 'a6f72972-502e-452b-9773-51699a527122';
});

这是工作原理。

看起来需要将这些字符串解析为JSON。为了提供一种解决方案,通过删除有问题的索引(而不是创建没有索引的副本)来实际改变detailsArray,这里有一种通过使用Array.prototype.reduce回调的indexOf

var detailsArray=[ {\id\:\b00a3a47-783a-4af5-90d9-59c4deb7a9e3\,\notes\:\sdfsdf\,\recordType\:0}, {\id\:\a6f72972-502e-452b-9773-51699a527122\,\notes\:\sdfsfdf\,\recordType\:0} ], detailId='a6f72972-502e-452b-9773-51699a527122'; var index=detailsArray.reducefunctionprev,curr,idx{ 返回prev==-1&&JSON.parsecurr.id===detailId? idx:prev; }, -1; 如果索引>-1{ detailsArray.index,1; } document.getElementById'out'。innerHTML+=JSON.stringifydetailsArray,null';
detailsArray=有没有理由让字符串数组看起来像对象而不是实际对象数组?请使用。代码:var newArr=arr.filterfunction{return JSON.parsee.id!==uglyId;}@NickZ是的,数组实际上来自Java映射,它以这种方式存储在我的后端。@Tushar我以前尝试过,但我的问题是element.id返回“undefined”。通过编辑感谢您的评论:我将尝试您编辑的评论,谢谢。@Tushar抱歉,我读了一篇没有JSON.parsee的旧评论。谢谢您的回答,菲尔,它正在工作!但是有一个问题,因为你提到了变异部分。。有没有一种方法可以在不改变原始数组的情况下使用JSON.parse?@hitch.united by mutate我的意思是,detailsArray本身是通过删除所需的索引来修改的,而不是创建一个没有找到索引的副本,如其他答案中使用filterOk所示。我理解,我现在在使用JSON.parseelement时遇到了一个问题。看起来它实际上正在将my Details Sarray中的对象更改为JSON,所以当它被保存时,它在java.@hitch.united中破坏了我的映射,那么你一定在其他地方更改了它。这个代码和另一个答案中的代码都不会改变数组值。如果你是对的,我在其他地方更改了它。非常感谢。
var index = detailsArray.reduce(function(prev, curr, idx) {
    return prev === -1 && JSON.parse(curr).id === detailId ?
        idx : prev;
}, -1);
if (index > -1) {
    detailsArray.splice(index, 1);
}