1

I have a code what take the next array:

const arr = [
    {
        name:'',
        age:55
    },
    {
        name:'Bill',
        age:''
    },
]

..and i should take into account, if the key does not contains a value, i should output for that key, the value: no data;
At the end i shoul get something like this:

const arr = [
    {
        name:'no data',
        age:55
    },
    {
        name:'Bill',
        age:'no data'
    },
]

I made this:

const arr = [
    {
        name:'',
        age:55
    },
    {
        name:'Bill',
        age:''
    },
]
const func = (arr) => {
const myKeys = arr.map(k => Object.keys(k))
   return  arr.map((i, k1) => {
        return myKeys[k1].reduce((acc,r )=> {
            return {
                [acc] : !i[r].toString().length ? 'no data' :i[r] 
            }
        })
       
    })
};
console.log(func(arr))

How to achieve what i described above?

5 Answers 5

1

Not sure if i understood your question, but this seems to work:

const arr = [
    {
        name:'',
        age:55
    },
    {
        name:'Bill',
        age:''
    },
]

arr.forEach(item => {
  for(let key in item) {
    if(!item[key]) item[key] = 'no data';
  }
});

console.log(arr);

Sign up to request clarification or add additional context in comments.

Comments

0

You could get the entries from the object, check the value and build a new object.

const
    array = [{ name: 'no data', age: 55 }, { name: 'Bill', age: 'no data' }],
    result = array.map(o => 
        Object.fromEntries(Object
            .entries(o)
            .map(([k, v]) => [k, v.toString() ? v : 'no data'])
        )
    );

console.log(result);

1 Comment

0

Solution with a double forEach:

const arr = [
    {
        name:'',
        age:55
    },
    {
        name:'Bill',
        age:''
    },
]
const func = (arr) => {
  arr.forEach((el) => {
    Object.keys(el).forEach((k) => el[k] = el[k].toString().length ? el[k] : 'no data')
  });
  return arr;
};
console.log(func(arr))

Comments

0

You're not updating the accumulator correctly in the reduce function. acc is the object you're building, not a property name. r is the current property name. So you should be assigning to acc[r].

You also need to provide an initial value argument {}.

You can also simplify the code by just calling Object.keys() inside the map function, rather than making a separate array of all the keys.

const arr = [{
    name: '',
    age: 55
  },
  {
    name: 'Bill',
    age: ''
  },
]
const func = (arr) => {
  return arr.map((i) => {
    return Object.keys(i).reduce((acc, r) => {
      acc[r] = !i[r].toString().length ? 'no data' : i[r];
      return acc;
    }, {})

  })
};
console.log(func(arr))

1 Comment

could you take a look here stackoverflow.com/questions/63047692/…
0

you can loop throw array object using map() then loop again throw object and see if object entry has value or not

arr.map(ele=>  {
  for (const [key, value] of Object.entries(ele)) {
        ele[key] = value ? value : 'no data';
    }
})

1 Comment

Please add some explanation to your answer such that others can learn from it

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.