I have an array of objects where some keys return an empty value. When an empty value appears I'd like to return that key as a string.
For example this array:
'products': [{
'name': 'Test product',
'id': '',
'price': '',
'brand': 'AwesomeBrand',
'colour': 'Gray',
'quantity': 1
},
{
'name': '',
'id': '123',
'price': '',
'brand': 'AwesomeBrand',
'colour': 'Black',
'quantity': 1
}]
In the example above I want to return the following:
'id, price, name'
I've tried the following that kind of does the job. However, it still returns as an array. Then I JSON.strigify it but that returns an ugly string. I tried replacing/RegEx cleaning it however, the program I'm using uses a Sandboxed Version of JavaScript so some features are not accessible.
I'm struggling to grasp the concept of accessing keys and values in an array of objects. So that is part of my problem.
var productArray = ecomValues || [];
var emptyArray = [];
var output = productArray.reduce(function (previousValue, currentValue, currentIndex) {
var keys = Object.keys(currentValue).filter(function (key) {
return !currentValue[key];/ });
if (keys.length) {
previousValue[currentIndex] = keys;
}
return previousValue;
}, {});
var emptyValues = output;
emptyArray.push(emptyValues);
Anyone that can help me with my issue?