0

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?

4
  • Do you want the string for each object in the array? Or all the keys combined? Commented Apr 8, 2022 at 9:10
  • All keys combined. So I prefer to have it return a single string with 'id, name' etc. Preferably comma separated. but you can use the .join for that Commented Apr 8, 2022 at 9:24
  • @Jorginton I added an answer. Please have a look. Hope that will work as per your expectation Commented Apr 9, 2022 at 6:59
  • Hey Rohit! Thank you! That sadly does not work since I can't use the Set method. Commented Apr 12, 2022 at 14:31

4 Answers 4

0

We can use reduce() to create an array with all the keys that are empty

  1. To find the empty keys we can use this for an example

  2. Then, we remove all duplicate keys

  3. And join() the array to a string

const data = {'products': [{'name': 'Test product', 'id': '', 'price': '', 'brand': 'AwesomeBrand', 'colour': 'Gray', 'quantity': 1 }, {'name': '', 'id': '123', 'price': '', 'brand': 'AwesomeBrand', 'colour': 'Black', 'quantity': 1 }]};

const emptyKeys = data.products.reduce((prev, cur) => 
    [ ...prev, ...Object.keys(cur).filter((key) => !cur[key]) ], [])
    .filter((v, i, a) => a.indexOf(v) === i)
    .join(', ');

console.log(emptyKeys);

id, price, name
Sign up to request clarification or add additional context in comments.

4 Comments

I do get an array when I execute it. I think the Sandboxed JS can't read the ... 1 Error(s) parsing the input: mismatched input '.' expecting {'~', 'function', 'null', 'typeof', 'undefined', 'getContainerVariable', String, Number, Boolean, '++', '--', '-', '!', Identifier, ']', '{', '[', '('} Offending token '.' at 11,6.
Did you run the snippet? Working fine, maybe you can add your code to your question?
The snippet works but the program can't read the dots (...). Is there a way around this?
Not sure what you mean by that. WE'll need to see your code to know what you're talking about.
0

Iterate products with Array.flatMap(), filter out all keys with non-empty values, and then make the array unique by using a Set, and spreading back to an array:

const products = [{ name: 'Test product', id: '', price: '', brand: 'AwesomeBrand', colour: 'Gray', quantity: 1 }, { name: '', id: '123', price: '', brand: 'AwesomeBrand', colour: 'Black', quantity: 1 }]

const result = [...new Set(products.flatMap(
  obj => Object.keys(obj)
    .filter(key => obj[key] === '')
))]

console.log(result);

Comments

0

You could iterate the array and get the keys and add the keys if a falsy value is found.

const
    products = [{ name: 'Test product', id: '', price: '', brand: 'AwesomeBrand', colour: 'Gray', quantity: 1 }, { name: '', id: '123', price: '', brand: 'AwesomeBrand', colour: 'Black', quantity: 1 }],
    empty = Array.from(products.reduce((s, o) => {
        Object.keys(o).forEach(k => {
            if (o[k] === '') s.add(k);
        });
        return s;
    }, new Set));

console.log(empty.join(', '));

A solution without Set.

const
    products = [{ name: 'Test product', id: '', price: '', brand: 'AwesomeBrand', colour: 'Gray', quantity: 1 }, { name: '', id: '123', price: '', brand: 'AwesomeBrand', colour: 'Black', quantity: 1 }],
    empty = Array.from(products.reduce((r, o) => {
        Object.keys(o).forEach(k => {
            if (o[k] === '' && !r.includes(k)) r.push(k);
        });
        return r;
    }, []));

console.log(empty.join(', '));

3 Comments

Thank you for your reply! I do get an error when I try to use the Set method: 1 Error(s) parsing the input: extraneous input 'Set' expecting {')', ','} Offending token '' at 15,7.
does it work above?
The snippet works! However, when I copy and paste it it says that it can't read the 'Set)' bit. Is there a way around the Set method?
0

You can simply achieve that by iterating the array using Array.forEach() along with Object.keys() method.

Demo :

const products = [{                           
  'name': 'Test product',     
  'id': '',
  'price': '',
  'brand': 'AwesomeBrand',
  'colour': 'Gray',
  'quantity': 1
}, {
  'name': '',
  'id': '123',
  'price': '',
  'brand': 'AwesomeBrand',
  'colour': 'Black',
  'quantity': 1
}];

const res = [];

products.forEach((obj) => {
    Object.keys(obj).forEach((objKey) => {
    !obj[objKey] ? res.push(objKey) : ''
  })
})

// Remove duplicate elements from an array
console.log([...new Set(res)]);

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.