Uncaught TypeError: Object - javascript

I want to search two item (name=string and location=json). this search is (one input box and two columns for search).
at the moment with this code I can find 'name' but i need I need to find location also.
if(textToCheck !== '') {
if((searchArray[i]['location']).toLowerCase().search(textToCheck) === -1) {
display = false;
}
}
the code that I suggest and doesn't work is:
if(textToCheck !== '') {
if((searchArray[i]['name']).toLowerCase().search(textToCheck) === -1 || (searchArray[i]['location']).toLowerCase().search(textToCheck) === -1) {
display = false;
}
}
error is :
Uncaught TypeError: Object 123 Street,xxx,xx,Canada,123rd Street,xxx,xx,123 xxx,12345 xxx,France has no method 'toLowerCase' FilterController.showFilteredSet (anonymous function)

As you said location=json, actually searchArray[i]['location'] is a object but not string. You need to do search depend on the what the object like.
Or simply change the object to string format like below:
JSON.stringify(searchArray[i]['location']).toLowerCase().search(textToCheck) === -1

JSON.stringify() is fine. But that searches in the object keys also.
This means:
if your "JSON" object looks like this:
({
street: 'my street',
country: 'Texas'
})
JSON.stringify(obj).toLowerCase().search('country') will find a result, even if the "data" doesn't contain it.
instead:
use a generalized way to do a flat search on objects.
Object.prototype.search = function(subject) {
for(var k in this) {
if(this.hasOwnProperty(k) && this[k].toString().toLowerCase().search(subject) !== -1)
return true;
}
return false;
};
var myObj = ({ foo: 'bar', hello: 'world' });
console.log(myObj.search('ar')); //search for "ar", returns true
console.log(myObj.search('ponyo!')); //search for "ponyo!", returns false
console.log(myObj.search('hello')); //search for "hello", returns false
in your case that would decline to:
//somewhere above, run this only once:
Object.prototype.search = function(subject) {
for(var k in this) {
if(this[k].toString().toLowerCase().search(subject) !== -1)
return true;
}
return false;
};
/////
if(textToCheck !== '') {
if((searchArray[i]['name']).toLowerCase().search(textToCheck) === -1 &&
(searchArray[i]['location']).search(textToCheck) === false) {
display = false;
}
}
please be warned that this code modifies the Object prototype, adding a "search" function to all objects (this might conflict with other libraries, you may or may not be using, that want to do the same).

Related

Object extensions in nodeJS

Is it possible to have object extensions in JavaScript? For example
Extensions.js
function any.isNullOrEmpty() {
if (this == null || this == "") {
return true
}
return false
}
app.js
var x = ""
console.log(x.isNullOrEmpty()) //should log true
is this possible? How do I do it?
You could add a method to the Object prototype, and use the valueOf method to get the value of the string:
...but, because null is a primitive that cannot have a method, the only way I can think of to get the target to be null would be to use call, apply or bind.
But you would never do this in production code, because modifying the prototype of built-in objects is discouraged.
'use strict' // important for the use of `call` and `null`
Object.prototype.isNullOrEmpty = function() { return this === null || this.valueOf() === '' }
const s = ''
console.log(s.isNullOrEmpty())
const t = null
console.log(Object.prototype.isNullOrEmpty.call(t))
You could use Object.prototype to extend this type of functionality in JavaScript.
Object.prototype.isNullOrEmpty = function() {
if (this == null || this == "") {
return true
}
return false
}
var x = "";
x.isNullOrEmpty(); // returns true
you need to add your custom method into prop type of object or array or everything u want to use your method on it.
but in your case you need to this like code below:
Object.prototype.isNullOrEmpty = function(){
if (this === null || this == "") {
return true
}
return false
}
let a = {a:'10'}
console.log(a.isNullOrEmpty())
function validateValue(value){
function isNullEmpty(){
return (value === void (0) || value == null)
}
return { isNullOrEmpty }
}
}

How to compare values of two objects after an API call, check for nulls or empties

I have an original object that is modified after an API call. I need to make sure that any fields that were originally not empty are reassigned to their original value. For example if articleTitle was initially filled out, and then after the API call it gets replaced with an empty value, I want to reassign it back to the original articleTitle value from the old object.
The two objects have the same keys, but I can't assume that the data coming back from the response is always going to be valid (but the original object always has valid data, that's why I need to reassign any empty fields to original values).
I (kinda) have a theoretically functional method, however I'm wondering if there is a more efficient way to do this. Here's what I have:
function evaluateEmptyValues = (originalReference, reference) {
// Get keys of both reference objects
var newReference = Object.entries(reference);
var oldReference = Object.entries(originalReference);
// Get length of both reference objects
var newReferenceLength = newReference.length;
var oldReferenceLength = oldReference.length;
// Double check objects are of the same length -- they always should be
if (newReferenceLength == oldReferenceLength) {
// Cycle through both objects
for (var i = 0; i < newReference.length; i++) {
console.log('i is ' + i);
// Again, these two lengths should be equal
if (newReference[i].length == oldReference[i].length) {
// Check if elements in current iteration is an object --
// if one is an object, then the other SHOULD also be
if ((typeof(newReference[i][j]) == 'object' &&
typeof(oldReference[i][j]) == 'object'
) {
// If both are objects, repeat lines 3 and 4
var currentNewReference = Object.entries(newReference[i][j]);
var currentOldReference = Object.entries(oldReference[i][j]);
// Get their lengths
var currentNewReferenceLength = currentNewReference.length;
var currentOldReferenceLength = currentOldReference.length;
// Both should be of the same length
if (currentNewReferenceLength == currentOldReferenceLength) {
for (var io = 0; io < currentNewReferenceLength.length; io++) {
console.log('io is ' + io);
// Both should also be of the same length
if (currentNewReference[io].length == currentOldReference[io].length) {
// For each iteration...
for (var jo = 0; jo < currentNewReference[io].length; jo++) {
// Check for empty values
if (currentNewReference[io][jo] == undefined ||
currentNewReference[io][jo] == null ||
(typeof(currentNewReference[io][jo]) == 'string' && currentNewReference[io][jo].trim() == '')
) {
// If empty, then reassign the empty value in the new reference
// object with the value of the field from the old reference
// object, regardless of whether or not the old value is also empty/null
currentNewReference[io][jo] = currentOldReference[io][jo];
}
}
} else {
// Serious problem
}
}
} else {
// Serious problem
}
} else {
// Cycle through current field
for (var j = 0; j < newReference[i].length; j++) {
// Check for nulls or empties
if (newReference[i][j] == undefined ||
newReference[i][j] == null ||
(typeof(newReference[i][j]) == 'string' && newReference[i][j].trim() == '')
) {
// Assign old value to new value, regardless of
// whether or not old value is also empty
newReference[i][j] = oldReference[i][j];
}
}
}
} else {
// Serious problem
}
}
} else {
// Serious problem
}
I doubt this is a very scalable or maintainable approach, and I'm wondering if there are any suggestions on enhancing this function, preferably using ES5, unless the ES6+ version works in most browsers.
For some reference, here are the two objects:
Here, articleTitle is empty.
Here, it is filled out from the API call. This is expected and needed, however imagine if it was the other way around, and articleTitle came back empty in the newReference after the API call
Edit:
Using the accepted answer plus an adjustment, this solved my specific problem:
function evaluateEmptyValues(reference, originalReference) {
var vm = this;
// Get keys and values of both reference objects
referenceLength = Object.entries(reference).length;
originalReferenceLength = Object.entries(originalReference).length;
if (referenceLength == originalReferenceLength) {
try {
// Cycle through both objects
for (var prop in reference) {
if (reference[prop] != undefined || reference[prop] != null) {
if (typeof (reference[prop]) == 'string' && reference[prop].trim() != '') {
// If both current elements are objects, recurse
if (typeof reference[prop] == 'object' && typeof originalReference[prop] == 'object') {
vm.evaluateEmptyValues(reference[prop], originalReference[prop])
}
// If both current elements are arrays, recurse
if (Array.isArray(reference[prop]) && typeof Array.isArray(originalReference[prop])) {
reference[prop].forEach((item, index) => vm.evaluateEmptyValues(item, originalReference[prop][index]));
}
// If new value is null, empty or undefined, assign it to old value,
// regardless of whether or not the old value was also null/empty.
//
///// This is to ensure that no existing previous values are
///// overwritten with any nulls or empty values
} else {
reference[prop] = originalReference[prop];
}
} else {
reference[prop] = originalReference[prop];
}
}
} catch(err) {
console.log(err);
}
}
console.log(reference);
You can simplify your function by a lot using recursion and a for ... in loop. I made two test objects to illustrate all the cases of your original example. In case it hits an array of objects it will iterate through that array and check for empty values recursively as well. Please see snippet below:
function evaluateEmptyValues(reference, originalReference) {
if (reference.length == originalReference.length) {
for (var prop in reference) {
if (typeof reference[prop] == 'object' && typeof originalReference[prop] == 'object') {
evaluateEmptyValues(reference[prop], originalReference[prop])
}
if (Array.isArray(reference[prop]) && typeof Array.isArray(originalReference[prop])) {
reference[prop].forEach((item, index) => evaluateEmptyValues(item, originalReference[prop][index]));
}
if (reference[prop] == undefined || reference[prop] == null ||
(typeof (reference[prop]) == 'string' && reference[prop].trim() == '')) {
reference[prop] = originalReference[prop];
}
}
}
}
const original = {
name: "Jack",
employee: {
firstName: "Nathan",
favoriteAnimal: {
species: "Donkey",
nature: "Lazy"
},
favoriteBeverages: [
{ name: "Beer", temperature: "Cold" },
{ name: "More beer", temperature: "Colder" }
]
},
occupation: "Plumber"
}
const newObject = {
name: "Jack",
employee: {
firstName: " ",
favoriteAnimal: {
species: null,
nature: "Lazy"
},
favoriteBeverages: [
{ name: "Beer", temperature: ""},
{ name: null, temperature: "Colder" }
]
},
occupation: undefined
}
evaluateEmptyValues(newObject, original);
console.log(newObject);
I think instead of using lot's of if conditions, you can try lodash, and use isEqual method which do a deep comparison between two values ( in your case two objects ), your code can be much cleaner as well.
var object = { 'a': 1 };
var other = { 'a': 1 };
_.isEqual(object, other);
// => true
You could make use of a recursive function. Something like this.
function mapper(oldObj, newObj) {
Object.entries(oldObj).forEach(([key, value]) => {
if (!newObj[key]) {
newObj[key] = value;
} else if (Array.isArray(newObj[key])) {
newObj[key].forEach((o, i) => mapper(oldObj[key][i], o));
} else if (Object.prototype.toString.call(newObj[key]) === "[object Object]") {
mapper(oldObj[key], newObj[key]);
}
});
return newObj;
}
const next = mapper(oldObj, newObj);
This will basically loop over all the items in the original object, and set the key/value in the new object if it doesn't exist.

In Node.js/Javascript, how to check if value exists in multidimensional array?

Lets say I have array something like this:
$game = Array
(
['round'] => Array
(
['match'] => Array
(
['player_2'] => Array
(
[name] => asddd
[id] => 1846845
[winner] => yes
)
['player_21'] => Array
(
[name] => ddd
[id] => 1848280
[winner] => no
)
)
)
)
And lets say in Node.js/Javascript I need to check if player_3 winner key value is yes. In PHP, you did something like this:
if( $game['round']['match'][player_3]['winner'] == 'yes'){
}
Because there is no player_3 it returns false in PHP, but if I did something similar in Javascript:
if( typeof game['round']['match'][player_3]['winner'] != 'undefined' && game['round']['match'][player_3]['winner'] == 'yes'){
}
I would get error: (node:15048) UnhandledPromiseRejectionWarning: TypeError: Cannot read property '0' of undefined, because player_3 doesn't exist in array and you can't check key value of array, that doesn't exist.
You could do this in Javascript:
if(
typeof game['round'] != 'undefined' &&
typeof game['round']['match'] != 'undefined' &&
typeof game['round']['match']['player_3'] != 'undefined' &&
typeof game['round']['match']['player_3']['winner'] != 'undefined' &&
typeof game['round']['match']['player_3']['winner'] == 'yes'
){
var playerIsWinner = true;
}else{
var playerIsWinner = false;
}
You check each array inside array from top down and make sure that they exists. I don't know if it actually works, but even if it does, it seems bad and stupid way to check something. I mean look how simple it is in PHP, while in Javascript I have to check each array existence inside array. So is there better way checking value of array?
Please, look at the lodash library, especially at methods, such as get. Then you may safely try something like:
_.get(game, 'round.match.player3.winner', false);
That's it :)
The array you've shown resembles more of an object in JS. So here's what you could possibly try.
const obj = {
round: {
match: {
player_2: {
name: 'asddd',
id: 1846845,
winner: true
},
player_21: {
name: 'ddd',
id: 1848280,
winner: false
}
}
}
}
...
const isWinner = (key) => {
try {
return obj.round.match[key].winner
} catch(e) {
return false
}
}
console.log(isWinner('player_2'))
By wrapping your return inside of try ... catch, you prevent the error from being thrown at your query and possibly stopping the app from continuing on. If from whatever reason the structure of your object would change, you'd still have a boolean value returned.
If you have to keep your winner value in form of a string, you could simply compare the value, and you'll end up with boolean as a result. As so:
const isWinner = (key) => {
try {
return (obj.round.matchs[key].winner === 'yes')
} catch(e) {
return false
}
}
No you don't need to do manual check for deeply nested data for null or undefined, instead you could rollout your own small but compact function in this case get and be flexible with any input data provided. See more about Array.prototype.reduce()
// this single line will do the trick
const get = (p, o) =>p.reduce((xs, x) => (xs && xs[x]) ? xs[x] : null, o)
// let's pass in our props object...
const props = {"round":{"match":[{"player_21":{"name":"asdfg","id":1846845,"winner":"not yet"}},{"player_2":{"name":"sany","id":1846280,"winner":"no"}},{"player_3":{"name":"sunny","id":1846000,"winner":"yes"}}]}}
var playerIsWinner = false;
//console.log(get(['round', 'match', 2, 'player_3', 'winner'], props))
if (get(['round', 'match', 2, 'player_3', 'winner'], props) == 'yes') {
playerIsWinner = true;
}
console.log(playerIsWinner)
You can do the following to avoid the undefined error:
if ( game['round']['match'][player_3] && game['round']['match'][player_3]['winner'] == 'yes' ) {
...
}
Because undefined is "falsey" you'll get false instead of undefined if player_3 does not exist, and true if player_3 exists and is the winner.
Just wrap in a try catch block. This allows you to skip all of your boilerplate typeof statements:
typeof game['round'] != 'undefined' &&
typeof game['round']['match'] != 'undefined' &&
typeof game['round']['match']['player_3'] != 'undefined' &&
typeof game['round']['match']['player_3']['winner'] != 'undefined' &&
typeof game['round']['match']['player_3']['winner'] == 'yes'
Instead throw an error and handle it if a element is not present. For example:
let nestedObj = {
prop1: {
propa1: {
propb1: 'test'
}
},
prop2: {}
}
try {
console.log(nestedObj['prop1']['propa1']['propb1']);
console.log(nestedObj['prop2']['propa2']['propb2']);
} catch(e) {
console.log('prop not defined');
}
This is a lot simpler than checking for every simple condition like you are doing now.

Check if nested JSON structure contains key

I'm trying to figure out how to check if a deeply nested JSON object, with several unknown arrays and properties contains a property that I'm looking for. I'm looking for a property that is called "isInvalid". If the field is there and the value of that key is true. I want to return false.
var checkValidity = function (data) {
for (var property in data) {
if (data.hasOwnProperty(property)) {
if (property == "isInvalid" && data[property] === true) {
return false;
}
else {
if (typeof data[property] === "object" && data[property] !== null) {
this.checkValidity(data[property]);
}
}
}
}
};
This is the code I've been trying out but I'm unable to get that to work. I have been looking into underscore also, but cant find the needed functions. Anyone has an idea? (No reg exp please)
If you really just want to check for property presence regardless of its particular location within JSON, then the easiest/fastest way is substring search in the source JSON string. If the latter is well-formed, then the property should be encoded in JSON as '"isInvalid":true'.
var checkValidity = function (jsonstr) {
return jsonstr.indexOf('"isInvalid":true') >= 0;
}
You can check like this
var s = {a:'1',b:'2'};
if(Object.getOwnPropertyNames(s).indexOf('a') != -1){
console.log('available');
}else{
console.log('Not available');
};
editing answer... UPDATE
var s = {
a1: '1',
b: '2',
c: {
a: '11'
}
};
var checkValidity = function (data) {
if (Object.getOwnPropertyNames(data).indexOf('a') != - 1) {
console.log('Found that key!!!');
} else {
for (var property in data) {
if (Object.getOwnPropertyNames(property).indexOf('a') != - 1) {
console.log('Found that key!!!');
} else {
if (typeof data[property] === 'object' && data[property] !== null) {
console.log('not found continue in inner obj..');
this.checkValidity(data[property]);
}
}
}
};
};
checkValidity(s);
It tests for every nesting level the property isInvalid and if not, all other properties as object and their content. Array#every breaks if one return is false.
function checkValidity(data) {
return !data.isInvalid && Object.keys(data).every(function (property) {
if (typeof data[property] === "object" && data[property] !== null) {
return checkValidity(data[property]);
}
return true;
});
}
var data = {
a: 1,
b: 2,
c: {
isInvalid: true,
a: false
}
};
document.write('checkValidity() should be false: ' + checkValidity(data) + '<br>');
data.c.isInvalid = false;
document.write('checkValidity() should be true: ' + checkValidity(data));
For complex json searching like this, I would use jsonpath ( http://goessner.net/articles/JsonPath/ ) which is the JSON equivalent of xpath.
To find the isInvalid field no matter where it is in the json, you would use it like this:
jsonPath(data, "$..isInvalid")

In Javascript, how can I check the existence of a specific key/value pair nested inside another key/value pair?

For example, this is the value of the object I am processing:
object = {"message":"success","dataList":{"state":"error","count":"25"}}
I know that to check if key "message" exists, I can do the following:
if(object['message']){
//"message" exists. do stuff.
} else{
//"message" does not exist
}
How do I check for the existence of "state" or "count" though?
if(object['dataList']['state']){
// dataList["state"] exists. do stuff.
} else {
// dataList["state"] does not exist
}
or the (in my opinion) more readable:
if(object.dataList.state){ ... }
Edit: It would also be a good idea to check for all parent objects so you will not get an unexpected error when, for example, dataList does not exist on the object:
if (object && object.dataList && object.dataList.state)
try like this:
object = {"message":"success","dataList":{"state":"error","count":"25"}};
if(object.message =='success'){
console.log(object.dataList);// your datalist object here use it
console.log(object.dataList.state);
console.log(object.dataList.count);
} else{
//"message" does not exist
}
if you are trying to check state do this:
if(typeof(object.dataList.state) !='undefined' && object.dataList.state.length > 0){
// dataList.state exists. do stuff.
} else {
// dataList.state does not exist
}
if (object.dataList.state || object.dataList.count) {
// ...
}
First of all, if you are checking the existence of a property, you should use if ('message' in object). If you use if (object['message']), for the case object = {'message': null}, the result is wrong.
In your question, the object is nested into two layers. If there are more than two, you can try a generic solution using recursion:
function findProperty (obj, key) {
if (typeof obj === "object") {
if (key in obj) return true;
var childReturned = false;
for (k in obj) {
childReturned = findProperty(obj[k], key);
if (childReturned) return true;
}
}
return false;
}
var obj = {"message":"success","dataList":{"state":"error","count":"25"}};
findProperty(obj, 'message'); // => true
findProperty(obj, 'dataList'); // => true
findProperty(obj, 'state'); // => true
findProperty(obj, 'count'); // => true

Categories

Resources