Today I’m going to share with you a little tool that takes the fields you want from a large JSON file and returns them as JSON. That’s a general description, right? Take it easy. Get right to the code.

Const setJson = (arr, object) => {let obj = {}; The switch (Object. The prototype. ToString. Call (arr)) {/ / an Array type case '[Object Array]' : arr.forEach(key => { obj[key] = object[key]; }); return obj; Case '[object object]': object.keys (arr). ForEach (key => {obj[key] = object[key]; }); return obj; }};Copy the code

If the first parameter is an array type, loop through the array to get the corresponding value, which is used as a key to fetch the value from the data source, assign the value to a new object, and return the new object. Similarly, if you pass in an Object type, the object.keys () method returns the key in that Object as an array, and then does the same thing for arrays.

Did you ever assign data this way?

The data source is here

then

This is fine, but if the object to which the assignment is to be made has a lot of properties in it, it can be tiring to write and the code is not concise. Try this little tool:

1. Throw an object in data directly

2. Throw in a custom array (the data source must contain the fields you want)

Or,

OK, the gadget eventually returns an object, if you are interested, you can try it out. If you have better or other tools to improve code quality and legibility, please share with us.