0

I Have an json array in the following format:

Object {field: "l0", headerText: "0-22", width: 120}
Object {field: "l1", headerText: "23-43", width: 120}
Object {field: "l2", headerText: "44-55", width: 120}

I want to delete a record on the basis of headerText value

i.e. I have value 0-22 and on the basis of this I have to delete this record. Is it possible?

4
  • 1
    Are these objects wrapped in an array ? Commented Mar 20, 2017 at 10:00
  • yes these are wrapped Commented Mar 20, 2017 at 10:02
  • 1
    Try this stackoverflow.com/a/15367759 Commented Mar 20, 2017 at 10:03
  • You can use the array's filter function to get the object/index and then remove it from array using Array.splice() Commented Mar 20, 2017 at 10:08

4 Answers 4

2

Assuming your objects are wrapped in an array, like this:

var arr = [
    {field: "l0", headerText: "0-22", width: 120},
    {field: "l1", headerText: "23-43", width: 120},
    {field: "l2", headerText: "44-55", width: 120}
]; 

You can do something like this:

var result = arr.filter(function(obj) {
    return obj.headerText !== "0-22"; // Or whatever value you want to use
});
console.log(result);
Sign up to request clarification or add additional context in comments.

Comments

1
Array.prototype.removeVal = function(name, value){
var array = $.map(this, function(v,i){
  return v[name] === value ? null : v;
});
this.length = 0; 
this.push.apply(this, array); //push all elements except the one we want to delete
}

var myArr = {};

myArray = [
{field: "l0", headerText: "0-22", width: 120},
{field: "l1", headerText: "0-11", width: 120},
{field: "l2", headerText: "0-33", width: 120}
];

myArray.removeVal('headerText', '0-11');

Fiddle

Comments

0
for(var i = 0; i<jsonArray.length; i++){
    if(jsonArray[i]. headerText == "0-22"){
       jsonArray.splice(i, 1);
    }
}

1 Comment

it will be a better answer if you describe your code there!
0

Look here.

var objects = [
  {field: 'l0', headerText: '0-22', width: 120},
  {field: 'l1', headerText: '23-43', width: 120},
  {field: 'l2', headerText: '44-55', width: 120}
];

function filterObjects(element, index) {
  if (element.headerText === '0-22') {
    objects.splice(index, 1);
  }
}

objects.forEach(filterObjects);

console.log(objects);

Comments

Your Answer

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.