1

I have an Object like this ;

{"0":0,"1":1,"2":2,"3":3,"4":4,"5":5,"6":"","7":"","8":"","9":6,"10":7,"11":"","12":8,"13":9,"14":10}

or initially an array :

[{"id":0,"tag":0},{"id":1,"tag":1},{"id":2,"tag":2},{"id":3,"tag":3},{"id":4,"tag":4},{"id":5,"tag":5},{"id":6,"tag":""},{"id":7,"tag":""},{"id":8,"tag":""},{"id":9,"tag":6},{"id":10,"tag":7},{"id":11,"tag":""},{"id":12,"tag":8},{"id":13,"tag":9},{"id":14,"tag":10}]

At the end, I want to retrieve an the same object but only with key:value with value is not = to zero.

like :

{"0":0,"1":1,"2":2,"3":3,"4":4,"5":5,"9":6,"10":7,"12":8,"13":9,"14":10}
TheDevGuy
  • 563
  • 4
  • 18
  • 2
    sorry im not following in your result the first one has value 0 – jstuartmilne Nov 13 '19 at 15:36
  • have you looked through https://stackoverflow.com/questions/14810506/map-function-for-objects-instead-of-arrays and if you wanted to switch it to an array - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map ? What you want to do is fairly simple after reading and understanding those 2 links. – Andrew Lohr Nov 13 '19 at 15:36
  • 2
    BTW this should be an array really – ibrahim mahrir Nov 13 '19 at 15:37
  • Your objects mimics an array. Just use `[0,1,2,3,4,5,'','','',6,7,'','',8,9,10]` and save yourself a lot of headaches in the future. Then you can use array.filter() to remove the blanks. – Shilly Nov 13 '19 at 15:40
  • You mean the `tag` property is not `0` or an empty string `""`, right? – ibrahim mahrir Nov 13 '19 at 15:41
  • 1
    Please visit [help], take [tour] to see what and [ask]. Do some research, search for related topics on SO; if you get stuck, post a [mcve] of your attempt, noting input and expected output. – mplungjan Nov 13 '19 at 15:41

5 Answers5

1

Just filter out objects with a falsy tag property (0 and empty strings are falsy):

let results = arr.filter(o => o.tag);

Example:

let arr = [{"id":0,"tag":0},{"id":1,"tag":1},{"id":2,"tag":2},{"id":3,"tag":3},{"id":4,"tag":4},{"id":5,"tag":5},{"id":6,"tag":""},{"id":7,"tag":""},{"id":8,"tag":""},{"id":9,"tag":6},{"id":10,"tag":7},{"id":11,"tag":""},{"id":12,"tag":8},{"id":13,"tag":9},{"id":14,"tag":10}];


let results = arr.filter(o => o.tag);

console.log(results);

If tag could have other falsy values that you want to keep (for example false or null) then change the filtering condition to a more explicit one:

let results = arr.filter(o => o.tag !== 0 && o.tag !== "");

Example:

let arr = [{"id":0,"tag":0},{"id":1,"tag":false},{"id":2,"tag":2},{"id":3,"tag":3},{"id":4,"tag":4},{"id":5,"tag":5},{"id":6,"tag":""},{"id":7,"tag":""},{"id":8,"tag":""},{"id":9,"tag":6},{"id":10,"tag":null},{"id":11,"tag":""},{"id":12,"tag":8},{"id":13,"tag":9},{"id":14,"tag":10}];


let results = arr.filter(o => o.tag !== 0 && o.tag !== "");

console.log(results);
ibrahim mahrir
  • 28,583
  • 5
  • 34
  • 61
0
const a = {"0":0,"1":1,"2":2,"3":3,"4":4,"5":5,"6":"","7":"","8":"","9":6,"10":7,"11":"","12":8,"13":9,"14":10};
let b = {};
Object.keys(a).filter(k => a[k]!=='').forEach(k => b[k]=a[k]);
console.log(b)
lcdev
  • 151
  • 5
0

Assuming you meant empty instead of 0 this is what you want

const sample = {"0":0,"1":1,"2":2,"3":3,"4":4,"5":5,"6":"","7":"","8":"","9":6,"10":7,"11":"","12":8,"13":9,"14":10};
Object.fromEntries(Object.entries(sample).filter(([k,v])=> v !== ''));

if you want this for a custom value you could build a simple function

const filterFromObjectWhereValueIs = (source, targetValue) => Object.fromEntries(Object.entries(source).filter(([k,v])=> v !== targetValue));

So for your example it will be something like

console.log(filterFromObjectWhereValueIs(sample,''))
jstuartmilne
  • 3,838
  • 1
  • 15
  • 22
0

Since you clearly stated that you want the same object, this is the way you can do it.

const input = {"0":0,"1":1,"2":2,"3":3,"4":4,"5":5,"6":"","7":"","8":"","9":6,"10":7,"11":"","12":8,"13":9,"14":10};

for (let key in input) {
  if (input[key] === '')
    delete input[key];
}

console.log(input);

alternatively, if you don't really need the same object, but a similar one, you can do something like this:

const input = {"0":0,"1":1,"2":2,"3":3,"4":4,"5":5,"6":"","7":"","8":"","9":6,"10":7,"11":"","12":8,"13":9,"14":10};

const result = Object.entries(input).filter(([key, value]) => value !== '').reduce((res, [key, value]) => {
  res[key] = value;
  return res;
}, {});

console.log(result);

Both solutions produce the same output but the first one does not change an object reference.

For the second type of input, just do this:

const input = [{"id":0,"tag":0},{"id":1,"tag":1},{"id":2,"tag":2},{"id":3,"tag":3},{"id":4,"tag":4},{"id":5,"tag":5},{"id":6,"tag":""},{"id":7,"tag":""},{"id":8,"tag":""},{"id":9,"tag":6},{"id":10,"tag":7},{"id":11,"tag":""},{"id":12,"tag":8},{"id":13,"tag":9},{"id":14,"tag":10}];

const result = input.filter(({tag}) => tag !== '');

console.log(result);
Sebastian Kaczmarek
  • 7,135
  • 4
  • 17
  • 36
0

You could filter the entries and get an object back.

var data = { 0: 0, 1: 1, 2: 2, 3: 3, 4: 4, 5: 5, 6: "", 7: "", 8: "", 9: 6, 10: 7, 11: "", 12: 8, 13: 9, 14: 10 },
    result = Object.fromEntries(
        Object
            .entries(data)
            .filter(([, v]) => v !== '')
    );

console.log(result);
Nina Scholz
  • 323,592
  • 20
  • 270
  • 324