Reputation: 1772
var jsonArray = ["test", "test1", "test2"];
var matchedValue = "test1";
I want to remove remove and (matchedValue) from the JsonArray. 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.
Upvotes: 0
Views: 24493
Reputation: 16
If you are using NodeJs, You can use 'modernarray' which is an small and simple npm package.
https://www.npmjs.com/package/modernarray
Install it by 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)
Upvotes: 0
Reputation: 1383
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:
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:
The filter() method creates a new array with all elements that pass the test implemented by the provided function.
https://developer.mozilla.org/tr/docs/Web/JavaScript/Reference/Global_Objects/Array/filter
Upvotes: 0
Reputation: 10912
// 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);
Upvotes: 5
Reputation: 26
Answer from what i got..
jsonArray.splice(jsonArray.indexOf('string_to_search'));
It will delete the found item and return remaining array.
Upvotes: 0