json-functions v1.0.10
json-functions
Downloads | Installation | Documentation | Testing
json-functions is a Js library to use array’s functions in Json objects.
Downloads 🔗
- Source code: src
- Js library: json-functions.js
Installation 📦
In a browser:
<script src="json-functions.js"></script>
Using npm:
npm i json-functions
Documentation 🧾
length | compare | forEach | map | mapValues | mapKeys | filter | reduce | reduceRight | every | some | keyOf | lastKeyOf | keysOf
@Params
@param obj
- A Json object.
@param callbackfn
- A function with the keys, values, obj and index of a Json as parameters.
- forEach calls the callbackfn function one time for each key in the Json.
@param thisArg
- An object to which the this keyword can refer in the callbackfn function.
- If thisArg is omitted, undefined is used as the this value.
@param searchElement
- The value to locate in the Json.
length
Gets the length of the Json. This is a number one higher than the highest element defined in the Json.
let lengthObj = { "bar1": "foo", "bar2": "foo", "bar3": "foo" };
let length = Json.length(lengthObj);
console.log(length); // => 3
compare
@param obj | @param obj
Transforms the object in strings and compare them
let compare1 = Json.compare({ "bar": "foo" }, { "bar": "foo" });
console.log(compare1); // => true
let compare2 = Json.compare([{ "bar": "foo" }], [{ "bar": "foo" }, "foo"]);
console.log(compare2); // => false
forEach
@param obj | @param callbackfn | @param thisArg
Performs the specified action for each element in a Json.
let forEachObj = { "bar1": "foo", "bar2": { "bar": "foo" }, "bar3": [1, 2, 3] };
Json.forEach(forEachObj, (key, value, obj) => {
console.log(key); // => bar1
console.log(value); // => foo
console.log(obj); // => {bar1: "foo", bar2: {…}, bar3: Array(3)}
});
The thisArg param won't work with arrow functions
function logJson(key, value) {
console.log({ this: this, key: key, value: value }); // => { this: "This example", key: "bar1", value: "foo" }
}
Json.forEach(forEachObj, logJson, "This example");
map
@param obj | @param callbackfn | @param keycallbackfn | @param thisArg
Calls a defined callback function and keycallbackfn function on each element of a Json, and returns a Json that contains the results.
let mapObject = { bar1: 1, bar2: 2, bar3: 3 };
let objectMapped = Json.map(
mapObject,
(key, value, obj) => value * 2,
(key, value, obj, index) => "newKey" + (index + 1)
);
console.log(objectMapped); // => {newKey1: 2, newKey2: 4, newKey3: 6}
mapValues
mapValues is the same as map but only has the value callback function.
let mapValuesObject = { bar1: 1, bar2: 2, bar3: 3 };
let objectValuesMapped = Json.mapValues(
mapValuesObject,
(key, value, obj) => value * 2
);
console.log(objectValuesMapped); // => {bar1: 2, bar2: 4, bar3: 6}
mapKeys
mapKeys is the same as map but only has the key callback function.
let mapKeysObject = { bar1: 1, bar2: 2, bar3: 3 };
let objectKeysMapped = Json.mapKeys(
mapKeysObject,
(key, value, obj, index) => "newKey" + (index + 1)
);
console.log(objectKeysMapped); // => {newKey1: 1, newKey2: 2, newKey3: 3}
filter
@param obj | @param callbackfn | @param thisArg
Returns the elements of a Json that meet the condition specified in a callback function.
let filterObject = { bar1: 1, bar2: 2, bar3: 3, bar4: 4, bar5: 5, bar6: 6 };
let filteredObject = Json.filter(filterObject, (key, value) => value % 2 == 0); // Filter values by even numbers
console.log(filteredObject); // => {bar2: 2, bar4: 4, bar6: 6}
let filteredObject2 = Json.filter(filteredObject, (key, value) => key == "bar1"); // Filter keys by "bar1"
console.log(filteredObject2); // => {}
reduce
@param obj | @param callbackfn | @param thisArg
Calls the specified callback function for all the elements in a Json. The return value of the callback function is the accumulated result, and is provided as an argument in the next call to the callback function.
let reduceObject = { bar1: 2, bar2: 4, bar3: 6, bar4: 8 };
//If initialValue is undefined acc is going to be equal to the first value of the Json
let reducedObject = Json.reduce(reduceObject, (acc, value, key, obj, index) => index != 0 ? acc + value : value);
//Initial value set to 2
let reducedObject2 = Json.reduce(reduceObject, (acc, value, key, obj, index) => acc + value, 2);
console.log(reducedObject); // => 20
console.log(reducedObject2); // => 22
reduceRight
@param obj | @param callbackfn | @param thisArg
Calls the specified callback function for all the elements in a Json, in descending order. The return value of the callback function is the accumulated result, and is provided as an argument in the next call to the callback function.
let reduceRightObject = { bar1: 2, bar2: 4, bar3: 6, bar4: 8 };
let reduceRightdObject = Json.reduceRight(reduceRightObject, (acc, value, key, obj, index) => {
console.log(acc, value); // => (100, 8) / (92, 6) / (86, 4) / (82,2)
return acc - value;
}, 100);
console.log(reduceRightdObject); // => 80
every
@param obj | @param callbackfn | @param thisArg
Determines whether all the members of a Json satisfy the specified test.
let everyObject = { bar1: 2, bar2: 4, bar3: 6, bar4: 8 };
let every = Json.every(everyObject, (key, value, obj, index) => value % 2 == 0);
console.log(every); // => true
let everyObject2 = { bar1: 1, bar2: 4, bar3: 6, bar4: 8 };
let every2 = Json.every(everyObject2, (key, value, obj, index) => value % 2 == 0);
console.log(every2); // => false
some
@param obj | @param callbackfn | @param thisArg
Determines whether the specified callback function returns true for any key or value of a Json.
let someObject = { bar1: 2, bar2: 4, bar3: 6, bar4: 8 };
let some = Json.some(someObject, (key, value, obj, index) => value == 8);
console.log(some); // => true
let someObject2 = { bar1: 1, bar2: 4, bar3: 6, bar4: 8 };
let some2 = Json.some(someObject2, (key, value, obj, index) => value == 10);
console.log(some2); // => false
keyOf
@param obj | @searchElement
Returns the key of the first occurrence of a value in a Json.
let keySearch = { "bar1": "foo", "bar2": "foo", "bar3": { "foo": "bar" } };
let keyOf = Json.keyOf(keySearch, { "foo": "bar" });
console.log(keyOf);
lastKeyOf
@param obj | @searchElement
Returns the key of the last occurrence of a specified value in a Json.
let keySearch2 = { "bar1": "foo", "bar2": "foo", "bar3": { "foo": "bar" } };
let lastKeyOf = Json.lastKeyOf(keySearch2, "foo");
console.log(lastKeyOf);
keysOf
@param obj | @searchElement
Returns an array of keys of a specified value in a Json.
let keySearch3 = { "bar1": "foo", "bar2": "foo", "bar3": { "foo": "bar" } };
let keysOff = Json.keysOf(keySearch3, "foo");
console.log(keysOff);
Testing
For testing you can clone the test folder wich contains a webpack project with a index.ts with all the examples above.
git clone https://github.com/codexjs/json-functions
cd json-functions/test
npm i
npm run test
Thanks to
This library would not have been possible without the support of fireship.io wich inspired me to build it, and it's videos were very helpful in all the development process. Any bug or improvement to add, just ask for it and I will try to add it.