xgqfrms™, xgqfrms® : xgqfrms's offical website of cnblogs! xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!

js array auto expand All In One

js array auto expand All In One


const arr = [
    {
        x: 1,
        y: 1,
        value:1,
    },
    {
        x: 2,
        y: 2,
        value: 2,
    },
    {
        x: 3,
        y: 3,
        value: 3,
    }
];


const autoExpand = (arr = []) => {
    let result = [];
    for (const obj of arr) {
        const len = obj.value;
        const temp = [...''.padStart(len, ' ')].map((item, i) => ({
            x: obj.x,
            y: obj.y,
            value: len,
        }));
        // console.log('temp =', temp);
        result = result.concat(temp);
    }
    return result;
};

const test = autoExpand(arr);

console.log('arr', arr);
console.log('test', test);



refs



©xgqfrms 2012-2020

www.cnblogs.com/xgqfrms 发布文章使用:只允许注册用户才可以访问!

原创文章,版权所有©️xgqfrms, 禁止转载 🈲️,侵权必究⚠️!


posted @ 2021-12-27 01:02  xgqfrms  阅读(21)  评论(0编辑  收藏  举报