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

asked Jan 9, 2018 at 7:43
2
  • 1
    JSON is always a string. if not you do not have a JSON. Commented Jan 9, 2018 at 7:45
  • 4
    Your code looks okay, check if fetchValue has data. setValue is an array. Commented Jan 9, 2018 at 7:46

4 Answers 4

5

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

answered Jan 9, 2018 at 7:54
Sign up to request clarification or add additional context in comments.

Comments

0

Answer from what i got..

jsonArray.splice(jsonArray.indexOf('string_to_search'));

It will delete the found item and return remaining array.

answered Jan 9, 2018 at 7:51

Comments

0

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

answered Jan 9, 2018 at 8:18

Comments

0

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)
answered Jul 25, 2021 at 14:23

1 Comment

Is this can be used in Vue.js?

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.