[英]Remove Json object from json array element
var jsonArray = ["test", "test1", "test2"];
var matchedValue = "test1";
I want to remove remove and (matchedValue) from the JsonArray.我想从 JsonArray 中删除 remove 和 (matchedValue)。 How can i delete and return rest of the string
我如何删除并返回字符串的其余部分
Expected output : ["test", "test2"];
What I am doing :我在做什么 :
var setValue = JSON.parse(fetchMethodJsonArray);
dataRemoved = setValue.filter(function(el) {
return el != fetchValue;
});
It is not working它不工作
Please tell me the solution Thanks in advance.请告诉我解决方案提前致谢。
// Assuming this is your fetched data const fetchMethodJsonArray = [{ "val": "One" }, { "val": "Two" }, { "val": "Three" }]; var setValue = fetchMethodJsonArray; const dataRemoved = setValue.filter((el) => { return el.val !== "One"; }); console.log(dataRemoved);
Answer from what i got..从我得到的回答..
jsonArray.splice(jsonArray.indexOf('string_to_search'));
It will delete the found item and return remaining array.它将删除找到的项目并返回剩余的数组。
You can use this:你可以使用这个:
const deleteObj = (data, column, search) => {
let result = data.filter(m => m[column] !== search);
return result;
}
When you want to remove an object from JSON array element use this method:当您想从 JSON 数组元素中删除对象时,请使用此方法:
const deleteObj = (data, column, search) => { let result = data.filter(m => m[column] !== search); return result; } const arr = [ { name: 'test', surname: 'test1' }, { name: 'test23', surname: 'newsname' }, { name: 'test23', surname: 'newsname' } ] const deleted = deleteObj(arr, 'name', 'test'); console.log(deleted);
In this sample, you will use filter method.在本示例中,您将使用过滤器方法。 Mozilla says about filter:
Mozilla 说关于过滤器:
The filter() method creates a new array with all elements that pass the test implemented by the provided function.
filter() 方法创建一个新数组,其中包含通过所提供函数实现的测试的所有元素。
https://developer.mozilla.org/tr/docs/Web/JavaScript/Reference/Global_Objects/Array/filter https://developer.mozilla.org/tr/docs/Web/JavaScript/Reference/Global_Objects/Array/filter
If you are using NodeJs, You can use 'modernarray' which is an small and simple npm package.如果您使用的是 NodeJs,您可以使用“modernarray”,它是一个小而简单的 npm 包。
https://www.npmjs.com/package/modernarray https://www.npmjs.com/package/modernarray
Install it by npm install modernarray
通过
npm install modernarray
安装它
let modernarray = require('modernarray');
let myarray = [{
"val": "One"
}, {
"val": "Two"
}, {
"val": "Three"
}];
let outputArray2 = modernarray.popByValue(myarray,{val:"One"})
console.log(outputArray2)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.