js function always returns null - javascript

This function always returns null...
function find1(arry, uid){
arry.forEach(function(obj){
if(obj.uid === uid){
return obj;
}
});
return null;
}
but when I change it to below code, it works...
function find2(arry, uid){
var dt;
arry.forEach(function(obj){
if(obj.uid === uid){
dt = obj;
}
});
return dt;
}
var array = [
{uid:"name01"},
{uid:"name02"},
{uid:"name04"},
{uid:"name04"}
];
console.log('find1', find1(array, 'name02')); // console output: find1 null
console.log('find2', find2(array, 'name02')); // console output: find2 Object {uid: "name02"}
what am I doing wrong in find1?

In find1 method you are explicitly returning null. And in forEach loop on arry value, it does not return from the find1 method, it returns from the anonymous function the inside forEach for all the values in array.

In first example you return return from internal function, not from function 'find1'. The result returns from internal function to body of 'find1'. But 'find1' doesn't return this result.
To fix it you only need a variable in function 'find1'. Like so:
var result = array.forEach(...);
return result;
Or so:
var result = null;
arry.forEach(function(obj){
if(obj.uid === uid){
result = obj;
}
});
return result;

Related

Check property exist in object or not in JavaScript?

I have one problem statement.
Implement a function propertyExists(obj, path) that takes in an object and a path (string) as arguments and returns ‘False’ if the property doesn’t exist on that object or is null, else returns the value of the property.
And here is solution.
function propertyExists(obj,path) {
// Write logic here
let result = obj.hasOwnProperty(path);
if(result)
{
return (obj.path);
}
else
{
return result;
}
}
Is this correct way of doing it?
Multiple issues:
the first name of the function should represent what it is doing,
Path as variable name is vague but propertyName as a variable is clear.
what you should do is either:
write function called, "getValue" it returns value if exist or null
function getValue(obj,propertyName) {
if(!obj) { // if object not exist
return null;
}
return obj[propertyName];
}
write function called, "propertyExists" should return true if exist else false.
function propertyExists(obj,propertyName) {
if(!obj) { // if object not exist
return false;
}
return obj.hasOwnProperty(propertyName);
}
function propertyExists(obj,path) {
// Write logic here
for(i in path){
if(obj.hasOwnProperty(path[i]) === true){
obj = obj[path[i]];
}else{
return false;
}
}
return obj;
}
object->obj,propertyname->path
function propertyExist(obj, path){
if (obj.hasOwnProperty(path)) return obj[path];
else return false;
};
Here, as far as I can understand, the objects could be nested also.
Let's take obj as {"a":{"b":"dadsa"}} and path as ab
Now, the the solution which you have posted will not work. Ideally, it should return dadsa, but it will return false.
Try the below code, it will work for nested objects also.
function propertyExists(obj,path) {
// Write logic here
var val=obj;
for(a of path){
val=val[a];
if(!val)
return false;
}
return val;
}
function propertyExists(obj,path) {
var val = obj;
for (a of path) {
val = val[a];
if (!val){
return false;
}
return val;
}

How to replace values of a dictionary in JS

Shouldn't this be creating a new dictionary and be able to replace its values everytime I call the getVal function?
var dict = [];
function getVal(inarr, element, data) {
var arr = inarr[element].map((item) => item[data]);
return arr;
};
console.log(getVal(event, 'foo', 'bar'));
function getVal(inarr, element, data) {
var arr = inarr[element].map((item) => item[data]);
return arr;
};
console.log(getVal(event, 'foo', 'bar'));
It's guess work at best because your question isn't clear and input is missing but you could try something like this:
//keys is an array of string or function
// if it's a function then it will pass current object
// to that function
function getMember(object,keys){
return keys.reduce(
function(acc,key){
if(acc===undefined){
return acc;
}
if(typeof key === "function"){
return key(acc);
}
return acc[key];
}
,object
)
}
function getVal(object) {
return getMember(
object,
[
"foo", // get member foo is an array of {bar:number}
function(x){ //map array of {bar:number} to array of number
return x.map(
function(x){
return x.bar;
});
}
]
);
};
var event = {
foo:[
{
bar:1
},
{
bar:2
}
]
};
console.log(getVal(event));// should log [1,2]

Using Array Map in a Javascript "Class" to Return Each Item of the Array

I'm trying to build some programs for fun and learning. I have the problem below:
function Hero(name) {
this.name = name;
this.addItem = function(item) {
this.items = this.items || [];
this.items.push(item);
};
// ....
this.viewItinerary = function() {
this.items.map(function(currItem){
return currItem;
});
};
}
var alex = new Hero("Alex");
alex.addItem("Sword");
alex.addItem("Shield");
console.log(alex.viewItinerary());
// returns undefined. why does it not return items in array?
If I replace the return statement with a console.log(currItem) it works. So why is my code returning undefined? Any ideas?
Thanks
Because your function is not returning anything
try:
this.viewItinerary = function() {
return this.items;
};
Also Array.map return a new array.
map returns a new array, unless you return the result of it the return type of viewItinery is undefined, hence why this line
console.log(alex.viewItinerary());
logs undefined. To fix it, simply add a return
this.viewItinerary = function() {
return this.items.map(function(currItem){
return currItem;
});
};

Test if Tree contains Object

I need a function to check if an object exists in a tree.
I recursively run through the tree and use lodash to check for equality of objects:
var objectInResultList = function (obj, list) {
list.forEach(function (item) {
if (_.isEqual(item, obj) === true) {
return true
}
else if (item.children.length > 0) {
return objectInResultList(obj, item.children);
}
});
return false;
};
var item = {"name":"Enterprise1.1","description": "testTest","children":[]};
var resultList = [{"name":"Enterprise1.1","description": "testTest","children":[{"name":"Enterprise1.1","description": "testTest","children":[]}]}];
var ret = objectInResultList(item, resultList);
alert(ret);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/1.2.1/lodash.min.js"></script>
The function should return true if the item is in the list and otherwise return false, but currently it always returns false.
Can someone find my problem?
JSFiddle
You are missing a couple of things.
A minor thing - your predicate is not returning false if the objects don't match and it has no children.
You are returning true/false from the predicate, but aren't using it anywhere so objectInResultList always returns false.
Rather than using forEach, it is easier to use find or findIndex, and the use the result of that to determine what to return from objectInResultList. For example if find returns undefined because of no match, then you return false.
In this case, you are using a tree with multiple lists, so using find makes more sense than findIndex.
var objectInResultList = function (obj, list) {
// call find to get the matching object
var match = list.find(function (item) {
if (_.isEqual(item, obj) === true) {
return true;
}
else if (item.children.length > 0) {
return objectInResultList(obj, item.children);
}
else {
return false;
}
});
// if match is undefined return false. If we found a match, return true
return !_.isUndefined(match);
};
var item = {"name":"Enterprise1.1","description": "testTest","children":[]};
var resultList = [{"name":"Enterprise1.1","description": "testTest","children":[{"name":"Enterprise1.1","description": "testTest","children":[]}]}];
var ret = objectInResultList(item, resultList);
alert(ret);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/1.2.1/lodash.min.js"></script>
forEach() has no return-value and it ignores any returned value. So returning true or false in the search-function is useless.
You should use a better iterator-function.
var objectInResultList = function (obj, list) {
function search(item){
if(item == null) return false;
if(_.isArray(item)) return _.some(item, search);
if(_.isEqual(item, obj)) return true;
return search(item.children);
}
return search(list);
};

Function with forEach returns undefined even with return statement

I'm just making a function for checking a value of something in my object array, but for some reason it keeps returning undefined. Why is that?
Demo: http://jsfiddle.net/cNYwz/1/
var data = [{
"Key": "1111-1111-1111",
"Email": "test#test.com"
}, {
"Key": "2222-2222-2222",
"Email": "test#boo.com"
}];
function getByKey(key) {
data.forEach(function (i, val) {
if (data[val].Key === key) {
return data[val].Key;
} else {
return "Couldn't find";
}
});
}
var asd = getByKey('1111-1111-1111');
console.log(asd);
In your function, you're returning from the function passed to forEach, not from getByKey.
You could adapt it like this :
function getByKey(key) {
var found = null;
data.forEach(function (val) {
if (val.Key === key) {
found = val;
}
});
return found;
}
But this would iterate over all elements, even if the item is immediately found. That's why you'd better use a simple for loop :
function getByKey(key) {
for (var i=0; i<data.length; i++) {
if (data[i].Key === key) {
return data[i];
}
}
}
Note that I also adapted your code to return the value, not the key. I suppose that was the intent. You might also have been confused with another iteration function : the first argument passed to the callback you give to forEach is the element of the array.
Your function getByKey has no return statement. The two returns are for the anonymous function used by forEach.
You're not returning anything to the outer scope, try this alternative:
function getByKey(key) {
var result = data.filter(function (i, val) {
return data[val].Key == key;
});
return result.length ? result : 'Not found';
}
Try storing the positive result as a variable, and then returning that variable (or a "Couldn't find" in case nothing is written) at the end of the function after the forEach loop.
function getByKey(key) {
var result;
data.forEach(function (val, i) {
if (data[val].Key === key) {
result = data[val].Key;
}
});
return result || "Couldn't find";
}
In addition to the ideas in the other answers, you're better off using Array.prototype.some, rather than forEach. That will let you stop when you find the first match:
function getByKey(key) {
var found = null;
data.some(function (val) {
if (val.Key === key) {
found = val;
return true; //stop iterating
}
});
return found;
}
You might also consider using filter, which can return an array containing just the objects where key matches:
function filter_array_by_key(key){
return data.filter(function(v){
return v.Key===key;
};
}
To get the first matching object, you can then use filter_array_by_key(key)[0], which will yield undefined if there was no match.

Categories

Resources