npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

transobj

v1.0.4

Published

easy to change Object's structor

Downloads

16

Readme

npm install transobj

demo

const Obj = {
    aa: 1,
    bb: ['12', '123', '888'],
    cc: [{
        aaa: '999',
        bbb: {
            ccc: '456'
        }
    }, {
        aaa: '1234',
        bbb: {
            ccc: '4567'
        }
    }],
    dd: {
        aa: '555',
        ddd: {
            eee: '666'
        }
    }
}

let x = transObj(Obj, {
    aa: Number,
    bb: Array,
    cc: [{
        aaa: String,
        bbb: {
            ccc: String
        }
    }],
    dd: {
        'aa|1': String,
        ddd: {
            eee: String
        }
    }
}, {
    xx: {
        aaa: 'aa|1',
        bbb: 'dd'
    },
    nn: {
        yy: [{
            uuu: 'bb',
            yyy: 'aaa'
        }]
    }
})

console.log(JSON.stringify(x))

可以根据将对象的结构轻松改变为你想要的结构,当然会有损耗性能,若对性能有极致的要求,不建议使用本方法(It is possible to easily change the structure of the object to the structure you want, and of course there will be lossy performance. If the performance is extremely demanding, this method is not recommended)。

只需要设置两次结构,即可轻松得到结果。同时也可让数据结构在代码中轻松可见(You only need to set up the structure twice to get the results easily. It also makes the data structure easy to see in the code.)。

  • 这是原对象(This is the original object)。
const transObj=require('transobj')

const Obj = {
    aa: 1,
    bb: ['12', '123', '888'],
    cc: [{
        aaa: '999',
        bbb: {
            ccc: '456'
        }
    }, {
        aaa: '1234',
        bbb: {
            ccc: '4567'
        }
    }],
    dd: {
        aa: '555',
        ddd: {
            eee: '666'
        }
    }
}
  • 第一步:先建立需要用到的数据结构,这样先检索得以优化性能,若key值重名了,则可将key值改写为'aa|1'(The first step: first establish the data structure that needs to be used, so that the first retrieval can optimize the performance. If the key value is the same, the key value can be rewritten as 'aa|1')。
{
    aa: Number,
    bb: Array,
    cc: [{
        aaa: String,
        bbb: {
            ccc: String
        }
    }],
    dd: {
        'aa|1': String,
        ddd: {
            eee: String
        }
    }
}
  • 第二步:建立想要得到的结果的数据结构,在值中设定源数据中对应的key值即可(Step 2: Establish the data structure of the desired result, and set the corresponding key value in the source data in the value)。
{
    xx: {
        aaa: 'aa|1',
        bbb:'dd'
    },
    nn: {
        yy: [{
            uuu: 'bb',
            yyy: 'aaa'
        }]
    }
}

- Here we go!这就是结果(This is the result)。

{
    "xx": {
        "aaa": "555",
        "bbb": {
            "aa": "555",
            "ddd": {
                "eee": "666"
            }
        }
    },
    "nn": {
        "yy": [{
            "uuu": "12",
            "yyy": "999"
        }, {
            "uuu": "123",
            "yyy": "1234"
        }, {
            "uuu": "888",
            "yyy": null
        }]
    }
}
  • 代码上只需要如下操作(Only the following operations are required on the code)。
let x = transObj(Obj, {
    aa: Number,
    bb: Array,
    cc: [{
        aaa: String,
        bbb: {
            ccc: String
        }
    }],
    dd: {
        'aa|1': String,
        ddd: {
            eee: String
        }
    }
}, {
    xx: {
        aaa: 'aa|1',
        bbb: 'dd'
    },
    nn: {
        yy: [{
            uuu: 'bb',
            yyy: 'aaa'
        }]
    }
})
  • 注意:暂不支持二维数组及以上的数据操作(Note: Data operations for 2D arrays and above are not supported at this time)。