0

I have an array of strings e.g. const array1 = ["124542", "112244", "112245", "112246"];

I want to create below results:

const array2 = [
  {
    "comparableId": "124542",
    "comparatorId": "112244"
  },
  {
    "comparableId": "112244",
    "comparatorId": "112245"
  },
  {
    "comparableId": "112245",
    "comparatorId": "112246"
  }
]

In this array2, 1st index value from array1 is with 2nd value, 2nd value with 3rd and 3rd with 4th and so on. This array1 is dynamic and can have just 1 value e.g. const array1 = ["124542"]; and can have any number of values.

I have tried using Ecma Script reduce and map methods.

const array2 = array1.map((value, index) => {
    return {
       comparableId: value,
       comparatorId: array1[index + 1] || '',
    };
});

Is there some other good way to do it ?

4 Answers 4

4

Using a for loop is probably the easiest here.

const array1 = ["124542", "112244", "112245", "112246"];


const transform = (array) => {
  const result = [];

  for (let i = 0; i === 0 || i < array.length - 1; ++i) {
    result.push({
      comparableId: array[i],
      comparatorId: array[i + 1] ?? "",
    })
  }
  
  return result;
};

console.log(transform(array1));
console.log(transform(["124542"]));

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

1 Comment

try this for a single value in the array1 e.g. const array1=["124542"]; It will give an empty array result. For that, we need to have array2 = [ { "comparableId": "124542", "comparatorId": "" } ]
0

Fully declarative, no mutations solution.

const
  array1 = ["124542", "112244", "112245", "112246"],
  array2 = [...Array(Math.ceil(array1.length / 2))].map((_, i) =>
    ({comparableId: array1[i * 2], comparatorId: array1[i * 2 + 1] ?? ""})
  );

console.log(array2);

2 Comments

some results are skipped, now.
What results?..
0

const array1 = ["124542", "112244", "112245", "112246"];

let array2 = [];

array1.map((item, index) =>{
  
  array2.push({comparableId: item, comparatorId: array1[index+1] || ""})
})
console.log(array2)

2 Comments

try this for a single value in the array1 e.g. const array1=["124542"]; It will give an empty array result. For that, we need to have array2 = [ { "comparableId": "124542", "comparatorId": "" } ]
already improved
0

A loop is the best option.

const array1 = ["124542", "112244", "112245", "112246"]
let array2 = [];
for (let i = 0; i < array1.length; i + 2) {
  array2[i] = {
    "comparableId": array2[i],
    "comparatorId": array2[i + 1]
  }
  console.log(array2[i])
}

array2 won't be a const variable as it is going to be modified each loop. If you really need it to be a constant then you'll have to define a new constant like so: const array3 = array2; at the end of your code.

2 Comments

"array2 won't be a const variable"? Yes it should be const, and each loop array2.push({...})
@TAHERElMehdi Wouldn't array2.push() result in an error because the value is a const? it has to be variable so that it can be modified by .push

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.