1

This part is responsible for processing the Data in the val array so i everything is an integer and saves it in the output array. (The val array sometimes contains floats and i cant work with those)

Lets say the next element that gets processed has a x and y pair that is already in the output array with any other color. How do i replace the old object with the new.

    val.forEach(element => {

        output.push({
            x: Math.round(element.x / element.width),
            y: Math.round(element.y / element.height),
            color: mapColorToBlock(element.color)

        })
    });



/* val array...
[
{"x":0,"y":0,"color":"blue","width":256,"height":256},
{"x":0,"y":256,"color":"blue","width":256,"height":256},
{"x":256,"y":256,"color":"blue","width":256,"height":256},
{"x":256,"y":0,"color":"blue","width":256,"height":256},
{"x":0,"y":256,"color":"lime","width":256,"height":256}
]
*/

/*output array after the processing(notice how there are 2 objects with x = 0 and y = 1 (the second and last entry in output))
[
  { x: 0, y: 0, color: 12 },
  { x: 0, y: 1, color: 12 },
  { x: 1, y: 1, color: 12 },
  { x: 1, y: 0, color: 12 },
  { x: 0, y: 1, color: 6 }
]
*/

It is important that the new one replaces the old object.(The new entry doesnt have to be in the same position in the array as the old one) In this scenario the output array would look like this.

[
  { x: 0, y: 0, color: 12 },
  { x: 1, y: 1, color: 12 },
  { x: 1, y: 0, color: 12 },
  { x: 0, y: 1, color: 6 }
]

Note: It is important that the new one replaces the old object

4 Answers 4

2
val.forEach(element => {
  const x = Math.round(element.x / element.width);
  const y = Math.round(element.y / element.height);
  const isDuplicate = output.some(item => item.x === x && item.y === y);
  
  if (!isDuplicate) {
    output.push({
        x: x,
        y: y,
        color: mapColorToBlock(element.color)

    })
  }

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

Comments

2

reduce over the array to create a new object with keys based on the x and y coordinates, and then use Object.values to create a new array of objects from that object's values.

const data=[{x:0,y:0,color:"blue",width:256,height:256},{x:0,y:256,color:"blue",width:256,height:256},{x:256,y:256,color:"blue",width:256,height:256},{x:256,y:0,color:"blue",width:256,height:256},{x:0,y:256,color:"lime",width:256,height:256}];

const out = data.reduce((acc, c) => {

  // Extract the values and create a key
  // from the x and y values
  const { x, y, color, width, height } = c;
  const key = `${x}|${y}`;

  // Do your x/y calculations
  const newX = Math.round(x / width);
  const newY = Math.round(y / height);

  // Add the current object to the property
  // defined by the key *replacing the data
  // if it already exists*
  acc[key] = { x: newX, y: newY, color };

  // Return the accumulator for the next iteration
  return acc;
}, {});

// Now extract the values from the new object
console.log(Object.values(out));

Comments

1

A modified version of Shuvo's answer.

This one is only sensible if mapColorToBlock can return different results in successive calls for the same value passed, otherwise Shuvo's answer which skips duplicates is better.

"It is important that the new one replaces the old object"...

let val = [
{"x":0,"y":0,"color":"blue","width":256,"height":256},
{"x":0,"y":256,"color":"blue","width":256,"height":256},
{"x":256,"y":256,"color":"blue","width":256,"height":256},
{"x":256,"y":0,"color":"blue","width":256,"height":256},
{"x":0,"y":256,"color":"lime","width":256,"height":256}
];

let output = [];

val.forEach(element => {
  const x = Math.round(element.x / element.width);
  const y = Math.round(element.y / element.height);
  const found = output.find(item => item.x === x && item.y === y);
  
  if (found) {
    found.color = mapColorToBlock(element.color);
  } else {  
    output.push({
        x: x,
        y: y,
        color: mapColorToBlock(element.color)
    })
  }
});

1 Comment

Shuvos answer just checked if the new entry is a duplicate and didnt replace the old one your answer worked for me. Thanks
1

Here you have to replace the previous element with same x and y

val.forEach(element => {
    output = [...output.filter(out => !( out.x === Math.round(element.x / element.width) && out.y === Math.round(element.y / element.height)), {
        x: Math.round(element.x / element.width),
        y: Math.round(element.y / element.height),
        color: mapColorToBlock(element.color)

    }]
});

Comments

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.