1

I have an array of objects and some values of the object are empty and I want to replace them with the values of the previous object, here is an example :

[
  {
    name: "Mark",
    surname: "Something",
  },
  {
    name: "",
    surname: "",
  },
  {
    name: "Richard",
    surname: "SomeSurname",
  },
  {
    name: "",
    surname: "",
  },
];

my goal is to replace the empty value with previous ones like that :

[
  {
    name: "Mark",
    surname: "Something",
  },
  {
    name: "Mark",
    surname: "Something",
  },
  {
    name: "Richard",
    surname: "SomeSurname",
  },
  {
    name: "Richard",
    surname: "SomeSurname",
  },
];
4
  • 4
    What have you tried so far to solve this on your own? A for loop would be enough. Commented Jul 2, 2021 at 12:14
  • Why is this tagged with node.js, json and csv? None of those tags are relevant for the question. Commented Jul 2, 2021 at 12:15
  • Show us what you have tried. SO isn't a free code writing service. The objective here is for you to post your attempts to solve your own issue and others help when they don't work as expected. See How to Ask and minimal reproducible example Commented Jul 2, 2021 at 12:15
  • because originally the data that I have is CSV, I am converting it with a nodejs package to have it as an object ;) Commented Jul 2, 2021 at 12:16

3 Answers 3

1

I'd run over the list and save the index of the last non-empty object os far, and then update the values to it when I encounter an empty one:

const list = [
  {
    name: "Mark",
    surname: "Something",
  },
  {
    name: "",
    surname: "",
  },
  {
    name: "Richard",
    surname: "SomeSurname",
  },
  {
    name: "",
    surname: "",
  },
];

let emptyIndex = 0;
for (let i = 0; i < list.length; ++i) {
  if (list[i].name) {
    fullIndex = i;
  } else {
    list[i].name = list[fullIndex].name;
    list[i].surname = list[fullIndex].surname;
  }
}

console.log(list);

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

Comments

1

const array = [
  {
    name: "Mark",
    surname: "Something",
  },
  {
    name: "",
    surname: "",
  },
  {
    name: "Richard",
    surname: "SomeSurname",
  },
  {
    name: "",
    surname: "",
  },
];

//create new array with map es6
let newArray = array.map( (obj,index) => { 
  if( !obj.name && !obj.surname ){
    //if values are empty, create a new one with the previous index
    return { name: array[index-1].name, surname: array[index-1].surname }
  }
  //else return original value
  return obj
})

console.log( newArray );

Comments

0

const a = [
  {
    name: "Mark",
    surname: "Something"
  },{
    name: "",
    surname: "" 
  },{
    name: "Richard",
    surname: "SomeSurname"
  },{
    name: "",
    surname: ""
  }
];
for (let i = 0; i < Math.trunc(a.length / 2); ++i) {
  a[2 * i + 1].name = a[2 * i].name;
  a[2 * i + 1].surname = a[2 * i].surname;
}
console.log(a);

1 Comment

Why does this question, that doesn't show any attempts of OP, deserve an answer?

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.