如何将[X:X1,Y:Y1]的数组转换为数字数组,其中数字被存储在JavaScript中,例如[X1,Y1,X2,Y2 ....]?

发布于 2025-02-10 19:31:43 字数 488 浏览 6 评论 0原文

我的数组看起来像:

const arr = [1,2,3,4,5,6]

我想将此数组转换为看起来像

const arr2 = [ {x: 1, y, 2}, {x:3, y:4}, {x:5, y:6}]

这样的东西:

let res: any = [];
const arr = [1,2,3,4,5,6]
for (let i = 0; i < arr.length / 2; i++) {
  let next = 0
  res.push({x : arr[i + next], y : arr[i+1+next]})
  next += 2;
}

console.log(res);

但这并不能给我正确的结果。

I have an array which looks like:

const arr = [1,2,3,4,5,6]

I want to convert this array to look something like

const arr2 = [ {x: 1, y, 2}, {x:3, y:4}, {x:5, y:6}]

I have tried something like this:

let res: any = [];
const arr = [1,2,3,4,5,6]
for (let i = 0; i < arr.length / 2; i++) {
  let next = 0
  res.push({x : arr[i + next], y : arr[i+1+next]})
  next += 2;
}

console.log(res);

But this doesnt give me the correct results.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

彩虹直至黑白 2025-02-17 19:31:43
const arr = [1, 2, 3, 4, 5, 6, 7]
var result = [];

for (var i = 0; i < arr.length; i += 2) {
  result.push({
    x: arr[i],
    y: i + 1 < arr.length ? arr[i + 1] : undefined
  })
}
console.log(result)

const arr = [1, 2, 3, 4, 5, 6, 7]
var result = [];

for (var i = 0; i < arr.length; i += 2) {
  result.push({
    x: arr[i],
    y: i + 1 < arr.length ? arr[i + 1] : undefined
  })
}
console.log(result)

花辞树 2025-02-17 19:31:43
convertToPoints = (arr) => {
    let arr2 = [];

    // if arr does not contain an even amount of elements, return false
    if (arr.length % 2 !== 0) {
        return false;
    }
    
    for (let i = 0; i < arr.length - 1; i += 2) {
        arr2.push({ x: arr[i], y: arr[i+1] });
    }

    return arr2;
}
convertToPoints = (arr) => {
    let arr2 = [];

    // if arr does not contain an even amount of elements, return false
    if (arr.length % 2 !== 0) {
        return false;
    }
    
    for (let i = 0; i < arr.length - 1; i += 2) {
        arr2.push({ x: arr[i], y: arr[i+1] });
    }

    return arr2;
}
标点 2025-02-17 19:31:43

您可以获得一对切成薄片的阵列,并创建具有短手属性的新对象。

const
    array = [1, 2, 3, 4, 5, 6],
    result = [];

let i = 0;
while (i < array.length) {
    const [x, y] = array.slice(i, i += 2);
    result.push({ x, y });
}

console.log(result);

一个简单的循环的更好方法。

const
    array = [1, 2, 3, 4, 5, 6],
    result = [];

for (let i = 0; i < array.length; i += 2) {
    result.push({ x: array[i], y: array[1 + 1] });
}

console.log(result);

You could get pairs of sliced arrays and create new objects with short hand properties.

const
    array = [1, 2, 3, 4, 5, 6],
    result = [];

let i = 0;
while (i < array.length) {
    const [x, y] = array.slice(i, i += 2);
    result.push({ x, y });
}

console.log(result);

A better approach with a simple loop.

const
    array = [1, 2, 3, 4, 5, 6],
    result = [];

for (let i = 0; i < array.length; i += 2) {
    result.push({ x: array[i], y: array[1 + 1] });
}

console.log(result);

尾戒 2025-02-17 19:31:43

您可以用减少

arr.reduce((acc,curr,index) => {
    if(index % 2) { 
        acc[acc.length-1].y = curr
        return acc
    }
    return acc.concat({x:curr})
}, [])

You can do this with reduce

arr.reduce((acc,curr,index) => {
    if(index % 2) { 
        acc[acc.length-1].y = curr
        return acc
    }
    return acc.concat({x:curr})
}, [])
千柳 2025-02-17 19:31:43

您可以加入数字,然后通过使用RegexP与函数string.prototype.match一起对坐标进行分组。

const arr = [1,2,3,4,5,6],
      result = arr.join("")
            .match(/([0-9]{2})/g)
            .map(coordinates => coordinates.split(""))
            .map(([x, y]) => ({x, y}));

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

You can join the numbers and then group the coordinates by using a Regexp along with the function String.prototype.match.

const arr = [1,2,3,4,5,6],
      result = arr.join("")
            .match(/([0-9]{2})/g)
            .map(coordinates => coordinates.split(""))
            .map(([x, y]) => ({x, y}));

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

ぺ禁宫浮华殁 2025-02-17 19:31:43

您错误地计算了索引。

let res: any = [];
const arr = [1,2,3,4,5,6]
for (let i = 0; i < arr.length; i += 2) {
  res.push({x : arr[i], y : arr[i+1]})
}

console.log(res);

You got the index computation wrong.

let res: any = [];
const arr = [1,2,3,4,5,6]
for (let i = 0; i < arr.length; i += 2) {
  res.push({x : arr[i], y : arr[i+1]})
}

console.log(res);

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文