0

I am filtering null values from an array of objects but receiving not a clean array as output

//returns ,cat,snake,dog,cat,snake
//var input1 = [{"dog":"35","cat":"21","girrafe":"33","snake":"44"},{"dog":"22","cat":"","girrafe":"2","snake":""},{"dog":"","cat":"","girrafe":"88","snake":""}];

//returns ",,dog,girrafe,snake"
//var input1 = [{"dog":"43","cat":"32","girrafe":"1","snake":"33"},{"dog":"1","cat":"23","girrafe":"1","snake":"23"},{"dog":"","cat":"5","girrafe":"","snake":""}];

//returns dog,,dog,cat,snake
//var input1 = [{"dog":"","cat":"s","girrafe":"1","snake":"54"},{"dog":"x","cat":"y","girrafe":"45","snake":"x"},{"dog":"","cat":"","girrafe":"1","snake":""}];

//tried to fix with .join() but then

//returns doggirrafecat,girrafe if I uses console.log(input1.join())
var input1 = [{"dog":"","cat":"s","girrafe":"1","snake":"54"},{"dog":"x","cat":"y","girrafe":"","snake":"x"},{"dog":"s","cat":"","girrafe":"","snake":"ss"}];


//returns typeof object
var emptyKeys = input1.map(function (object) {
    return Object.keys(object).filter(function (key) {
        return object[key] === '';
    });
});

console.log(emptyKeys)

So my goal is to get empty values in array of objects and get their keys. What am I doing wrong here and how do I fix it so that all inputs are return correctly?

VLAZ
  • 26,331
  • 9
  • 49
  • 67
John Long
  • 39
  • 8
  • I'm not sure what you are trying to do. Are you looking to get all empty `strings` in the array? – Manuel Abascal Oct 08 '20 at 04:54
  • yes get empty values in array of objects and get their keys – John Long Oct 08 '20 at 05:10
  • 3
    @JohnLong what is your expected output? It seems to be fine as your map will return an empty array for an object who does not have any keys with empty value. – Dipen Shah Oct 08 '20 at 05:14
  • The format of the the result that is being returned is a problem, I want object like "dog,cat,snake,dog" but I get sometimes weird commas for different inputs. – John Long Oct 08 '20 at 05:17
  • Are you turning your result to a string? Because that's how you'd get extra commas. You have an array *of arrays* and when you turn it into a string each array inside will also be turned into a string. You need to flatten the top array. – VLAZ Oct 08 '20 at 05:19
  • 1
    @VLAZ yes, as a string all I want to return from these inputs is one array in the best scenario but string is fine too. – John Long Oct 08 '20 at 05:22
  • Then [flatten your array](https://stackoverflow.com/questions/10865025/merge-flatten-an-array-of-arrays) and turn it into a string. – VLAZ Oct 08 '20 at 05:23
  • `flattenArrEmptykeys = emptyKeys.flat(2);` – Manuel Abascal Oct 08 '20 at 05:26
  • 1
    awesome. thanks!! I used var flatArray = Array.prototype.concat.apply([], input1); because I am on ES5 – John Long Oct 08 '20 at 05:26
  • If you need to turn into a `string`, you can use this instead: `flattenArrEmptykeys = emptyKeys.flat(2).toString();` – Manuel Abascal Oct 08 '20 at 05:27

3 Answers3

1

Flattening the array solved it,

var flatArray = Array.prototype.concat.apply([], input1);

Thanks for the suggestions!!

John Long
  • 39
  • 8
0
var emptyKeys = input1.map(function (object) {
    return Object.keys(object).filter(function (key) {
        return object[key] === '';
    });
}).filter(function(item){
 return item.length !== 0;
});

Is that what you mean?

0
input1.reduce(function(accumulator, currentValue){
    accumulator.push(...Object.entries(currentValue).filter((kvPair) => kvPair[1] === '').map(ele => ele[0]));
    return accumulator;
}, [])
Aditya Prasoon
  • 241
  • 2
  • 9