0

I have an object that I have converted to an array using this

convertToArray(data: any) {
  let arr = [];
  Object.keys(data).map(function(key){
     arr.push(data[key]);
  });
  return arr;
}

Here is the result

{0: {…}, 1: {…}, 2: {…}, hideTitle: false, template: "football-news", customProperties: {…}}
0: {NewsID: 90, AnotherAttribute: 5, …}
1: {NewsID: 90, AnotherAttribute: 5, …}
2: {NewsID: 90, AnotherAttribute: 5, …}
customProperties: {template: "list-view"}
hideTitle: false
template: "news"
__proto__: Object

to

(6) [{…}, {…}, {…}, false, "football-news", {…}]
0: {NewsID: 90, AnotherAttribute: 5, …}
1: {NewsID: 90, AnotherAttribute: 5, …}
2: {NewsID: 90, AnotherAttribute: 5, …}
3: false
4: "news"
5: {template: "list-view"}
length: 6
__proto__: Array(0)

I want to remove any items (arr.pop or filter) that DOESNT have a NewsID key.

I appreciate any help in advance :)

Jack Bashford
  • 43,180
  • 11
  • 50
  • 79
John Mersal
  • 187
  • 1
  • 2
  • 10

5 Answers5

3

You can use .filter() with destructuring assignment to filter out values which are undefined by only keeping objects where the NewsID gives a truthy value (here NewsID will be undefined if it does not exist, and thus falsey).

Moreover, you can refine your convertToArray method by using Object.values(). There is no need to loop over your keys/values twice.

See working example below:

function convertToArray(data) {
  return Object.values(data);
}

const myObj = {
  0: {
    NewsID: 90,
    AnotherAttribute: 5
  },
  1: {
    NewsID: 90,
    AnotherAttribute: 5
  },
  2: {
    NewsID: 90,
    AnotherAttribute: 5
  },
  customProperties: {
    template: "list-view"
  },
  hideTitle: false,
  template: "news"
},
arr = convertToArray(myObj),

res = arr.filter(({NewsID}) => NewsID);
console.log(res);

Note:

  • If your NewsID can be 0 then you should check using .filter(({NewsID}) => NewsID !== undefined).

  • If your NewsID can have an explicit value of undefined (eg: you use {NewsID: undefined}) you should use .filter((obj) => obj instanceof Object && "NewsID" in obj)

Nick Parsons
  • 45,728
  • 6
  • 46
  • 64
0

I might have misunderstood your question because printouts as images are really hard to read.


So it looks like you have a list of objects and you want to remove ones that don't have a certain key, correct?

You can it in following manner:

result = arr.filter(obj => "NewsID" in obj)

If you want to learn more about how to check if an object has a key, this looks like good place to start: Checking if a key exists in a JavaScript object?

Rikkokiri
  • 85
  • 8
0

Use a simple Boolean filter upon Object.values of the object:

const data = {
  0: {
    NewsID: 90,
    AnotherAttribute: 5
  },
  1: {
    NewsID: 90,
    AnotherAttribute: 5
  },
  2: {
    NewsID: 90,
    AnotherAttribute: 5
  },
  customProperties: {
    template: "list-view"
  },
  hideTitle: false,
  template: "news"
};

const arr = Object.values(data).filter(({ NewsID }) => NewsID);

console.log(arr);
Jack Bashford
  • 43,180
  • 11
  • 50
  • 79
0

You have an array which has values of diffrent types and diffrent types of object. First you need to check whether value in array is instance of a object or not.

function convertToArray(data) {
  let arr = [];
  Object.keys(data).map(function(key){
     arr.push(data[key]);
  });
  return arr;
}
var obj = {
   0:  { NewsID: 90, AnotherAttribute: 5},
   1: {NewsID: 90, AnotherAttribute: 5}, 
   2: {NewsID: 90, AnotherAttribute: 5}, 
   hideTitle: false, 
   template: "football-news", customProperties: {template: "list-view"}
};
var arr = convertToArray(obj);
var filterarr = arr.filter(a => a instanceof Object && ("NewsID" in a));
console.log(filterarr);
vaibhav shanker
  • 173
  • 2
  • 10
0

Try this :

var arr = [
  {NewsID: 90, AnotherAttribute: 5},
  {NewsID: 90, AnotherAttribute: 5},
  {NewsID: 90, AnotherAttribute: 5},
  false,
  "football-news",
  {template: "list-view"}
];

var result = arr.filter(item => item instanceof Object && ("NewsID" in item));
console.log(result);
Debug Diva
  • 26,058
  • 13
  • 70
  • 123