How to pass a function an object and a property - javascript

I want to create the following method:
// expects: (array of objects, name of property on object to compare
to value, value to compare to)
// returns: index of object in array
function findIndex(array, delegate, value) {
for (index in array) {
if (value === array[index].delegate) {
return index;
}
}
};
The above method does not work if I pass in a delegate value that is a string. Is something like this possible?

You can use bracket notation, like so
if (value === array[index][delegate]) {
return index;
}

Related

Function empty object check

I need help this task
You have to implement function that will
Check that object is empty.
Object considered to be empty when it has no properties or all its properties are false (I.e. 0, “”, false …)
check object for presence: {},0, “”, symbol(“key”), null, 5,[].
You can use something like this.
This will just loop through the object and check if any value exist and if it does it will just return false else it will return true.
function isEmpty(obj) {
for (let key in obj) {
if(!!obj[key]) return false;
}
return true;
}
You can use 'Object.Values(obj)' which returns an array of all the values from the key value pairs of the object passed as parameter.
'!Object.Values(obj)' will return false if there are no values, which means this is not an object.
'anArray.every(element => { //return a boolean } )' will loop through all the items of the array and return true only if the the arrow function returns true for all 'every' element on the array
function isEmpty (obj){
return !Object.Values(obj) ||
Object.Values(obj).every (v => !v));

findKey() - recreating lodash library method

I've got a problem with a CodeCademy task. I am to re-create the findKey lodash library method. Here there are the steps of how to do it, but I got stuck, especially at point 5.
Add a method to our _ object called findKey.
Add two parameters to this method: object and predicate. We will
name our predicate function parameter predicate since this is the
name used in the Lodash documentation.
Within the method, use a for ... in loop to iterate through each key
in object.
Within the loop, create a variable called value and set it equal to
the value at the current key in object.
Still within the loop, create another variable called
predicateReturnValue and set it equal to the result of calling
predicate with value.
Finally, still within the loop, use an if statement to check
if predicateReturnValue is truthy. If it is, return the current key
from the method.
Outside of the loop, return undefined to address all cases where no
truthy values were returned from predicate.
This is my code that doesn't work:
findKey(object, predicate) {
for (let key in object) {
let value = object[key];
let predicateReturnValue = predicate(value);
if (predicateReturnValue === 'true') {
return value;
};
};
return undefined;
}
I appreciate your help!
You need to return the key after the truty check of the call of predicate.
function findKey(object, predicate) {
for (let key in object) {
let value = object[key];
let predicateReturnValue = predicate(value);
if (predicateReturnValue) { // just take the value
return key; // return key
}
}
}
const
isStrictEqual = a => b => a === b,
object = { a: 'foo', b: 'bar', c: 'baz' }
console.log(findKey(object, isStrictEqual('bar')));
console.log(findKey(object, isStrictEqual('cat')));

countBy Method into a Function using Javascript

I'm trying to write a countBy function, identical to the countBy method in Javascript. Here is what I have so far:
var array = [1,2,4,4,5];
function countBy(collection, func) {
var object = {} ;
for (var i=0;i<=0; i++) {
for (var key in object) {
if (func(collection[i]) === object.key) {
object.key ++;
} else {
object.key = 1;
}
}
}
return object
}
alert(countBy(array, function(n) {return Math.floor(n);}));
What the code intends to do is search through the collection array to see if a value there matches a key in object. If it has found a match in the collection array, increment that key value by one. If it has not found one, create a new key value. Therefore, the result that should be alerted is: {4:2, 1:1, 2:1, 5:1}. But it seems like my output is [object Object]. What am I doing wrong??
for (var i=0;i<=0; i++) {
This will run exactly once instead of iterating through the collection.
for (var key in object) {
if (func(collection[i]) === object.key) {
This does the wrong thing; you should call func only once in each iteration and then look up the result of the function call in your object.
object.key ++;
This doesn't do what you want; it's better to use the array dereference operator instead.
function countBy(collection, func)
{
var object = Object.create(null);
collection.forEach(function(item) {
var key = func(item);
if (key in object) {
++object[key];
} else {
object[key] = 1;
}
});
return object;
}

Typescript function always returns parent object and not return value

I have a Typescript object that defines a function. The object itself defines an array of some objects. So I am trying to add a function to act more like an indexer. The function always returns the parent object where it is defined and not a specific string value.
Here's the code. The variables is defined as an Array of object having name and value as properties.
valueOf(key: string): string {
var result = '';
if (key === null || key === undefined || key.length === 0) {
return result;
}
for (var i = 0; i < this.variables.length; i++) {
if (this.variables[i].name === key) {
result = this.variables[i].value;
break;
}
}
return result;
}
UPDATE
The above function is defined on a Typescript object. This object includes a property of type Array. I want to query that array based on a string key and not number key. That's why I wrote the above function to loop over the elements of property "variables", i.e. the Array, when there is a match on "name" === "key passed in", return the "value" associated with the object inside "variables property".
for instance, I can use the above as:
var tsObj = new TsObj(...);
tsObj.valueOf('key1');

Function to get index from an array of objects having certain value of provided property

My question is based on and similar to this one but a little bit different because property name will be variable.
How do I create a function which will return me index of object having certain value of provided property?
function indexOf(propertyName,lookingForValue,array){
//......
return index;
}
So,
indexOf("token",123123,[
{id_list:1, name:'Nick',token:'312312'},{id_list:2,name:'John',token:'123123'}
]);
should return 1.
The main problem I have is how do I check the property value when I have the property name as string with me?
function indexOf(propertyName,lookingForValue,array) {
for (var i in array) {
if (array[i][propertyName] == lookingForValue) {
return i;
}
}
return undefined;
}
Edit: Please note that I do the loose type check '==' on purpose since you are giving an integer to this function whereas in the array the value you search for is a string.
I make a function which can be helpful to you. Check it.
function GetindexOf(propertyName,lookingForValue,array){
var obj = array;
for(o in obj)
{
if(obj[o][propertyName] == lookingForValue)
{
//return index;
alert("You have request for "+o+" index");
}
}
}

Categories