1.0.3 • Published 4 years ago

obj2array v1.0.3

Weekly downloads
2
License
ISC
Repository
github
Last release
4 years ago

obj2array

Convert json object to array, map each value and index position

NPM version License npm node

How was it work?

Parsed json object and converted an array to map, u can got each value and index position in that object.

Es5 browser supported, see

./src/main.js

Installation

npm i obj2array -s

Usage

const obj2array = require('obj2array');
const jsonParser = obj2array.jsonParser;

let json = {
    "testNum": 5,
    "testString": "tester",
    "testBool": true,
    "testArray": [1, 2, 3, 4],
    "testObj": {
        "num": 5,
        "string": "tester",
        "bool": true,
        "obj": {
            "a": "123",
            "b": "456",
            "c": {
                "d": "111",
                "e": 123,
                "aa": [1, 2, 3, 4]
            }
        },
        "obj2": {
            "g": 1,
            "gg": '1111'
        },
        "array": [1, 2, 3, 4]
    }
};

map

// map each children value and index position
jsonParser(json, function (val, idx) {
    console.log( 'index', idx ); // ["testObj"]["obj"]["c"]["aa"][3]
    console.log( 'value', val ); // value 4
    // ...
});

obj2array.reset(); // this function is not a constructor function, so you need reset defined when program completed this called.

set each value

// set value to each children key
jsonParser(json, function (val, idx) {
    eval( "json"+idx + " = '"+ idx +"'" );
});

console.log( JSON.stringify( json ) );


obj2array.reset(); // this function is not a constructor function, so you need reset defined when program completed this called.

TODO

1.0.3

4 years ago

1.0.2

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago