简体   繁体   中英

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. 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:

 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

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM