1

I have an array with multiple JSON objects. I don't need all key/values in my file so I want to delete e.g. all "Completed" keys and their values across all of those JSON objects. I know I could delete specific key/value pairs with the delete operator though leaving undefined holes in the array. I don't want that because I think it will cause problems later on when I work with the array.

I also tried it with splice:

data.splice(data[i].Completed, data.length);

Did I make a mistake with splice?

Acutally I want to create a new file without certain key/values. In my approaches I just manipulated the existing file without creating a new one … how could I do that?

var data = [
{
    "ID": 1,
    "Titel": "ui sketch",
    "Completed": "yes",
    "Prio": 3,
    "Importance": 2
  },
  {
    "ID": 2,
    "Titel": "coding",
    "Completed": "yes",
    "Prio": 4,
    "Importance": 4
  },
  {
    "ID": 5,
    "Titel": "meeting",
    "Completed": "no",
    "Prio": 3,
    "Importance": 2
  },
]
5
  • JSON objects? really strings? Commented May 26, 2017 at 8:28
  • 1
    please add what youi want, the same array with deleted objects, or just a new array without completed items? Commented May 26, 2017 at 8:30
  • 1
    splice would be for removing array data, not key/value in your objects. If you only want to remove the "Completed": "yes" key/value, and not the entire object containing that value, splice wouldn't do the trick. Commented May 26, 2017 at 8:31
  • I want the same array with deleted key/value pairs e.g. "Completed":"yes" and "Completed":"no". But I want to also do this with more complex key/value pairs that differ a lot from each other in each object e.g. "latitude":"123.23324.21", "longitutde":"3432.23242.31" Commented May 26, 2017 at 8:50
  • @Randy what would do the trick then? When I try it with map() the browser doesn't load the file ... Could this be due to my 200.000 objects in the array? Is that just to much for map()? Commented May 27, 2017 at 14:23

5 Answers 5

2

If you need to remove all objects with key 'Completed' equals 'yes' you do:

const newData = data.filter(el => el.Completed !== 'yes')

If you need to remove all keys Completed in case they are equal to 'yes', you do:

const newData = data.map(el => {
  if (el.Completed === 'yes') delete el.Completed
  return el;
})

In most common case for objects you use delete and for Arrays you use splice.

Sign up to request clarification or add additional context in comments.

Comments

0
var data = [
{
    "ID": 1,
    "Titel": "ui sketch",
    "Completed": "yes",
    "Prio": 3,
    "Importance": 2
  },
  {
    "ID": 2,
    "Titel": "coding",
    "Completed": "yes",
    "Prio": 4,
    "Importance": 4
  },
  {
    "ID": 5,
    "Titel": "meeting",
    "Completed": "no",
    "Prio": 3,
    "Importance": 2
  },
]

var i =data.length-1;
for(i ; i>= 0;i--){
    if(data[i].Completed == "yes"){
        data.splice( i, 1 );
    }
}
alert(data[0].Titel);

here you have it, search for completed == "yes" , if so , splice it, you end up with an array with 1 element :) hope it helps

Comments

0

What I understood from your question is that you want to delete all Completed properties from the objects in the data array.

In that case, you can use Array.prototype.map() to make a new array with objects that have the properties you want:

var data = [{"ID": 1,"Titel": "ui sketch","Completed": "yes","Prio": 3,"Importance": 2},{"ID": 2,"Titel": "coding","Completed": "yes","Prio": 4,"Importance": 4},{"ID": 5,"Titel": "meeting","Completed": "no","Prio": 3,"Importance": 2}],
    result = data.map(elem => {
      return {
        ID: elem.ID,
        Titel: elem.Titel,
        Prio: elem.Prio,
        Importance: elem.Importance
      };
    });

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

1 Comment

Thanks Yosvel. Your solution works on my given example. Unfortunately it doesn't seem to work on very large datasets. +50mb … The browser is unable to load the file …
0

Try this :

var data = [
{
    "ID": 1,
    "Titel": "ui sketch",
    "Completed": "yes",
    "Prio": 3,
    "Importance": 2
  },
  {
    "ID": 2,
    "Titel": "coding",
    "Completed": "yes",
    "Prio": 4,
    "Importance": 4
  },
  {
    "ID": 5,
    "Titel": "meeting",
    "Completed": "no",
    "Prio": 3,
    "Importance": 2
  }
];

var data = data.map(item => { 
  return {
    "ID": item.ID,
    "Titel": item.Titel,
    "Prio": item.Prio,
    "Importance": item.Importance
  }
});

console.log(data);

Comments

0

You can use Array.prototype.filter() methods: documentation on MDN.

Here what it looks like:

var data = [
{
    "ID": 1,
    "Titel": "ui sketch",
    "Completed": "yes",
    "Prio": 3,
    "Importance": 2
  },
  {
    "ID": 2,
    "Titel": "coding",
    "Completed": "yes",
    "Prio": 4,
    "Importance": 4
  },
  {
    "ID": 5,
    "Titel": "meeting",
    "Completed": "no",
    "Prio": 3,
    "Importance": 2
  },
]

// New array from former one.
var undone = data.filter(obj => obj["Completed"] != "yes");

// Skim data from completed elements
data = data.filter(obj => obj["Completed"] != "yes");

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.