skip to Main Content

As you can see I am printing object property values but I can’t print double underscore instead of ‘__’ missing property value.

function findAddress(obj){
let object = Object.values(obj);
for(let i = 0; i < obj.length; i++){
    const objects = obj[i];
    if(objects[values] === object[values] == ''){
        return objects.obj[values] = '__' ;
        }
    else if(objects === object){
        object = objects;
     }
}
return object;
}
const output = {street : 10, house : '', society : 'Earth Perfect'};
const res = findAddress(output);
console.log(res);

I am trying to print object property values but, if any property value is missing I want to print double underscore ‘__’ instead of missing value. This program is printing object property value but it didn’t printing missing property value.

2

Answers


  1. You can simply achieve this:

    function findAddress(obj){
      for(let property in obj){
        if(obj[property] === ''){
          obj[property] = '__';
        }
      }
      return obj;
    }
    const output = {street : 10, house : '', society : 'Earth Perfect'};
    const res = findAddress(output);
    console.log(res);
    Login or Signup to reply.
  2. You cannot iterate an object (it doesn’t have length property by default and number keys). But you can get an array of an object’s keys and reduce them to a new object with needed transformations:

    const findAddress = obj => 
      Object.keys(obj).reduce((r, key) => (r[key] = obj[key] === '' ? '__' : obj[key], r), {});
    
    const output = {street : 10, house : '', society : 'Earth Perfect'};
    const res = findAddress(output);
    console.log(res);
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search