Infinity if check function javascript [duplicate] - javascript

This question already has answers here:
How can I access and process nested objects, arrays, or JSON?
(31 answers)
Closed 2 years ago.
I have a JSON file with data and I want to create a function that inserts to a list only the values of each object.
How can I write this code to run unlimited times and do the same,
check if the value is not an object push it to list, or if it's an object he needs to do the same check.
Here is the code example, I just looking for a better way to write this, thanks!
JSON Data
{
"el1": "Custom Text1",
"el2": "Custom Text2",
"el3": {
"el4": "Custom Text3",
"el5": {
"el6": "Custom Text4",
"el7": {
"el8": "Custom Text5",
"el9": {
"el10": {
"el11": {
"el12": "Custom Text6"
}
}
}
}
}
}
}
JS Code
const data = require('./data.json');
let list = [];
Object.values(data).forEach(v => {
if(typeof v !== 'object') return list.push(v);
Object.values(v).forEach(v => {
if(typeof v !== 'object') return list.push(v);
Object.values(v).forEach(v => {
if(typeof v !== 'object') return list.push(v);
Object.values(v).forEach(v => {
if(typeof v !== 'object') return list.push(v);
Object.values(v).forEach(v => {
if(typeof v !== 'object') return list.push(v);
Object.values(v).forEach(v => {
if(typeof v !== 'object') return list.push(v);
Object.values(v).forEach(v => {
if(typeof v !== 'object') return list.push(v);
});
});
});
});
});
});
});
console.log(list)

const list = [];
const pushToList = (obj) => {
const values = Object.values(obj);
for (const value of values) {
if (typeof value === 'object') {
pushToList(value);
} else {
list.push(value);
}
}
};
pushToList(data);
console.log(list);

Related

A way to convert an object with keys of . seperated strings into a JSON object

I'm trying to figure out a way to turn and object like this :
{ "test.subtest.pass" : "test passed", "test.subtest.fail" : "test failed" }
into JSON like this:
{ "test": { "subtest": { "pass": "test passed", "fail": "test failed" }}}
sometimes there may be duplicate keys, as above perhaps there would be another entry like "test.subtest.pass.mark"
I have tried using the following method and it works but it's incredibly ugly:
convertToJSONFormat() {
const objectToTranslate = require('<linkToFile>');
const resultMap = this.objectMap(objectToTranslate, (item: string) => item.split('.'));
let newMap:any = {};
for (const [key,value] of Object.entries(resultMap)) {
let previousValue = null;
// #ts-ignore
for (const item of value) {
// #ts-ignore
if (value.length === 1) {
if(!newMap.hasOwnProperty(item)) {
newMap[item] = key
} // #ts-ignore
} else if (item === value[value.length - 1]) {
if(typeof previousValue[item] === 'string' ) {
const newKey = previousValue[item].toLowerCase().replace(/\s/g, '');;
const newValue = previousValue[item];
previousValue[item] = {};
previousValue[item][newKey] = newValue;
previousValue[item][item] = key;
} else {
previousValue[item] = key;
}
} else if (previousValue === null) {
if (!newMap.hasOwnProperty(item)) {
newMap[item] = {};
}
previousValue = newMap[item];
} else {
if (!previousValue.hasOwnProperty(item)) {
previousValue[item] = {}
previousValue = previousValue[item];
} else if (typeof previousValue[item] === 'string') {
const newValue = previousValue[item];
previousValue[item] = {};
previousValue[item][item] = newValue;
} else {
previousValue = previousValue[item];
}
}
}
}
return newMap;
}
We can utilize recursion to make the code a little less verbose:
function convertToJSONFormat(objectToTranslate) {
// create root object for the conversion result
const result = {};
// iterate each key-value pair on the object to be converted
Object
.entries(objectToTranslate)
.forEach(([path, value]) => {
// utilize a recursive function to write the value into the result object
addArrayPathToObject(result, path.split("."), value);
});
return result;
}
function addArrayPathToObject(root, parts, value) {
const p = parts.shift();
// base-case: We attach the value if we reach the last path fragment
if (parts.length == 0) {
root[p] = value
return;
}
// general case: check if root[p] exists, otherwise create it and set as new root.
if(!root[p]) root[p] = {};
addArrayPathToObject(root[p], parts, value)
}
This function utilizes the fact that objects are pass-by-reference to recursively traverse through the object starting at its root until setting the desired value.
You can add error-handling and other such concerns as necessary for your use.
#Meggan Naude, toJson function copies json object to reference obj for provided keys and value.
const p = { "test.subtest.pass" : "test passed", "test.subtest.fail" : "test failed" };
const result = {} ;
const toJson = (obj, keys, value) => {
if (keys?.length === 1) {
obj[keys[0]] = value;
return obj
} else {
const k = keys.splice(0, 1)
if (k in obj) {
toJson(obj[k], keys, value)
} else {
obj[k] = {};
toJson(obj[k], keys, value)
}
return obj
}
}
Object.keys(p).forEach(key => toJson(result, key.split('.'), p[key]))
console.log(result);

Get value of symbol defined inside object literal and iterate over its values

I have an object literal like below.
{
is_active: true,
start_date: { [Symbol(lt)]: 2020-02-24T12:56:36.780Z },
expiry_date: { [Symbol(gt)]: 2020-02-24T12:56:36.780Z },
[Symbol(or)]: [ { user_id: 'M' }, { user_id: null } ]
}
These objects are automatically generated by sequelize before querying to database
I need to iterate through all object keys and change the value of that key which is id or ends with _id.
This is my first time with Symbol datatype. I read a article here, It says they can't be iterate using for...in or object...keys
I also read an answer here, but It only said how to access it.
Below is my function that iterate recursively though the object and decrypts id and value of keys ending with _id
function decryptIds(obj) {
if (typeof obj === 'object' && obj !== null) {
for (let key in obj) {
if (Array.isArray(obj[key])) {
for(let i = 0; i< obj[key].length; i++) {
if(typeof obj[key][i] === 'object' && obj[key][i] !==null)
decryptIds(obj[key][i].where)
else
obj[key][i] = decrypt(obj[key][i])
}
} else if (typeof obj[key] === 'object' && obj !== null) {
decryptIds(obj[key].where)
}
else if (key === 'id' || key.endsWith('_id')) {
obj[key] = decrypt(obj[key])
}
}
}
return
}
decryptIds(model.where)
Somehow, I managed to do it. I know it's not the solution, it's easily feasible. I will keep try until I get any better approach. For now it is okay.
beforeFind: (model, options) => {
function decryptIds(obj) {
if(typeof obj === 'object' && obj!==snull &&Object.getOwnPropertySymbols(obj)) {
const objectSymbols = obj[Object.getOwnPropertySymbols(obj)[0]]
if(objectSymbols) {
objectSymbols.forEach(el => {
decryptIds(el)
})
}
}
if (typeof obj === 'object' && obj !== null) {
for (let key in obj) {
if (Array.isArray(obj[key])) {
for(let i = 0; i< obj[key].length; i++) {
if(typeof obj[key][i] === 'object' && obj[key][i] !==null)
decryptIds(obj[key][i].where)
else
obj[key][i] = decrypt(obj[key][i])
}
}
else if (key === 'id' || key.endsWith('_id')) {
if(obj[key]) {
obj[key] = decrypt(obj[key])
}
} else if (typeof obj[key] === 'object' && obj !== null) {
decryptIds(obj[key].where)
}
}
}
return
}
console.log(model.where)
decryptIds(model.where)
console.log(model.where)
},

DRY way to throw the same TypeError in more than one functions

I have some functions that each one uses an object as an argument.
All these objects have similar structure, so instead of making the same checks in each function, I've created some check functions:
const checkIfOptionsIsObject = options => {
if (typeof options === 'object') {
return true;
} else {
throw new TypeError('The function argument should be an object.');
}
}
and
const checkOptionsMinMax = options => {
if (isNaN(options.min) || isNaN(options.max)) {
throw new TypeError("'min' and 'max' should both be numbers.");
} else if (options.min > options.max) {
throw new Error("'min' shouldn't be greater than 'max'");
} else {
return true;
}
}
And here is how I am using them:
const firstFunction = options => {
checkIfOptionsIsObject(options);
checkOptionsMinMax(options);
// do some stuff
return result;
}
const secondFunction = options => {
checkIfOptionsIsObject(options);
checkOptionsMinMax(options);
// do some other stuff
return result;
}
const thirdFunction = options => {
checkIfOptionsIsObject(options);
// do some stuff that doesn't use min and max
return result;
}
Is there any problem in this code?
1) note that in your first check typeof object === 'object' if that variable named object is actualy an array it will also give you the type of 'object'. You can see this in your console by entering typeof [ ] === 'object' and notice that it'll return true. It's better to use o !== null && typeof o === 'object' && Array.isArray(o) === false; for object testing.
const checkIfOptionsIsObject = options => {
if (options !== null && typeof options === 'object' && Array.isArray(options) === false) {
return true;
} else {
throw new TypeError('The function argument should be an object.');
}
}

How use array in Angular JS?

I have initializated array in Angular JS:
$scope.formData.universitySelected = [];
I try to fill array in loop:
angular.forEach($scope.formData.university, function (value, key) {
if (typeof value === 'object') {
$scope.formData.universitySelected[key].id = value.IdEducation;
} else {
$scope.formData.universitySelected[key].selected = value;
}
});
But I get error:
Cannot set property 'id' of undefined
You should create an object before using it.
angular.forEach($scope.formData.university, function (value, key) {
$scope.formData.universitySelected[key] = $scope.formData.universitySelected[key] || {};
if (typeof value === 'object') {
$scope.formData.universitySelected[key].id = value.IdEducation;
} else {
$scope.formData.universitySelected[key].selected = value;
}
});
I think you need to define that object first then add value to in it.
Code
angular.forEach($scope.formData.university, function (value, key) {
if (typeof value === 'object') {
$scope.formData.universitySelected[key] = {id : value.IdEducation};
} else {
$scope.formData.universitySelected[key] = {selected : value.IdEducation};
}
});

traversing a json for empty array value

i have a below json
{
"loanDetails": [
{
"vehicleDetail": {
"RCBookImageReferences": {
"imagePathReferences": [
{
}
]
}
},
"chargeDetails": [
{
}
],
"commissionDetails": [
{
}
],
"disbursementDetails": [
{
}
]
}
]
}
in the above json i need to traverse every key and if i find it emty then set the parent as empty array ie the output should be as below
{"loanDetails":[]}
i used the code below
function isEmpty(obj) {
for(var prop in obj) {
if(obj.hasOwnProperty(prop))
return false;
}
return true;
}
But it did not give me the expected result.I'm stuck here any help will be much helpful.
The function clean takes an object and loops over its keys, calling clean recursively
on each object-valued property.
If the result of cleaning is an empty object, delete the key in question.
If the object itself turns out to be empty, return undefined, triggering deletion of the property holding that object at the higher level.
function clean(obj) {
var isEmpty = true;
for (var key in obj) {
var val = obj[key];
if (val === null || typeof val !== 'object' || (obj[key] = clean(val))) {
isEmpty = false;
} else {
delete obj[key];
}
}
return isEmpty ? undefined : obj;
}
>> a = { x: 1, b: { y: [] }, c: { d: { } } }
>> clean(a)
<< Object {x: 1}
This should make it recursive. With two solutions.
Solution 1: empty test function
var boolValue = true;
for(var prop in obj) {
if(obj.hasOwnProperty(prop) && typeof obj[prop] === 'object')
{
boolValue = recursiveIsEmpty(obj[prop]);
}
else
{
return false;
}
}
return boolValue ;
//test and set empty string
recursiveIsEmpty(jsonDataObj['loanDetails']) ? jsonDataObj['loanDetails'] = [] : null;
Solution 2 recursive empty function that empties parent obj
function recursiveIsEmpty(obj) {
var boolValue = true;
for(var prop in obj) {
if(obj.hasOwnProperty(prop) && typeof obj[prop] === 'object')
{
boolValue = recursiveIsEmpty(obj[prop]);
if (boolValue)
{
delete obj[prop]; //an object is empty. Delete from parent;
}
}
else
{
return false;
}
}
return boolValue; //returns an empty object
}
recursiveIsEmpty(jsonDataObj['loanDetails']) //returns jsonDataObj['loanDetails'] = [];
This checks if obj has a property that is an object. If so load that object and check it's properties. If not return false, because that will be string or number and that confirms the object is not empty.
Your JSON-string is not valid. When corrected, you can use a reviver function parameter (see MDN) to remove 'empty' arrays (aka properties with criteria you specify).
To be clear, the reviver function takes care of the traversing on all levels of the parsed object. If it returns undefined the property is removed from the object. The reviver used in the snippet thus removes all properties containing arrays with empty objects, or empty arrays.
The snippet demonstrates this.
// json string corrected
var foo = '{"loanDetails": [{"vehicleDetail": {"RCBookImageReferences": '+
'{"imagePathReferences": [{}]}}, "chargeDetails": [{}],'+
'"commissionDetails": [{}],"disbursementDetails": [{}]}]}';
// parse it, using reviver parameter
var fooparsed = JSON.parse( foo,
function (key, value) { //<= reviver here
return (value.length && value.length == 1 &&
value[0] instanceof Object &&
Object.keys(value[0]).length == 0) ||
value instanceof Array && !value.length
? undefined : value;
}
);
// print
Helpers.log2Screen( Object.print(fooparsed) );
<script src="http://kooiinc.github.io/JSHelpers/Helpers-min.js"></script>
if you are doing this using ajax then you should go with seriallizing the jason array using javascript.
at the time of passing data through json
data: "your data",
use this
data:$(form).serialize(),
it will pass all the key of that form which you are passing ,
if you want to see its result the try to print it on console
var inputObj = {
"loanDetails": [{
"vehicleDetail": {
"RCBookImageReferences": {
"imagePathReferences": [{}]
}
},
"chargeDetails": [{}],
"commissionDetails": [{}],
"disbursementDetails": [{}]
}, {
"vehicleDetail": {
"RCBookImageReferences": {
"imagePathReferences": [{
"Valid": "Working"
}]
}
},
"chargeDetails": [{}],
"commissionDetails": [{}],
"disbursementDetails": [{}]
}],
"Superman": {
"Name": ""
},
"SpiderMan": {
"Name": "Senthil"
}
}
function flatten(target, opts) {
var output = {},
opts = opts || {},
delimiter = opts.delimiter || '.'
function getkey(key, prev) {
return prev ? prev + delimiter + key : key
};
function step(object, prev) {
Object.keys(object).forEach(function(key) {
var isarray = opts.safe && Array.isArray(object[key]),
type = Object.prototype.toString.call(object[key]),
isobject = (type === "[object Object]" || type === "[object Array]")
if (!isarray && isobject) {
return step(object[key], getkey(key, prev))
}
output[getkey(key, prev)] = object[key]
});
if (Object.keys(object) == "") {
if (object instanceof Array) {
output[prev] = [];
} else {
output[prev] = {};
}
}
};
step(target)
return output
};
function unflatten(target, opts) {
var opts = opts || {},
delimiter = opts.delimiter || '.',
result = {}
if (Object.prototype.toString.call(target) !== '[object Object]') {
return target
}
function getkey(key) {
var parsedKey = parseInt(key)
return (isNaN(parsedKey) ? key : parsedKey)
};
Object.keys(target).forEach(function(key) {
var split = key.split(delimiter),
firstNibble, secondNibble, recipient = result
firstNibble = getkey(split.shift())
secondNibble = getkey(split[0])
while (secondNibble !== undefined) {
if (recipient[firstNibble] === undefined) {
recipient[firstNibble] = ((typeof secondNibble === 'number') ? [] : {})
}
recipient = recipient[firstNibble]
if (split.length > 0) {
firstNibble = getkey(split.shift())
secondNibble = getkey(split[0])
}
}
// unflatten again for 'messy objects'
recipient[firstNibble] = unflatten(target[key])
});
//Array Check
var keys = Object.keys(result);
if (keys.length > 0 && keys[0] === "0") {
var output = [];
keys.forEach(function(key) {
output.push(result[key])
});
return output;
}
return result
};
var flatted = flatten(inputObj);
var keys = Object.keys(flatted);
keys.forEach(function(key) {
if (JSON.stringify(flatted[key]) === "{}" || JSON.stringify(flatted[key]) == "") {
// console.log(key)
delete flatted[key];
var paths = key.split(".");
if (paths.length >= 2) {
var int = parseInt(paths[1])
if (isNaN(int)) {
key = paths[0];
flatted[key] = {};
} else {
key = paths[0] + "." + int;
flatted[key] = {};
}
var newKeys = Object.keys(flatted);
for (var j = 0; j < newKeys.length; j++) {
var omg = newKeys[j];
if (omg.indexOf(key) != -1 && omg.length > key.length) {
delete flatted[key];
}
}
}
}
})
console.log(flatted)
var output = unflatten(flatted);
alert(JSON.stringify(output))

Categories