Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

285
Views
How to return keys that have empty values from an array of objects as a string in JavaScript?

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?

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

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(', '));

about 4 years ago · Juan Pablo Isaza Report

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
about 4 years ago · Juan Pablo Isaza Report

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);

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!