This question already has answers here:
Filtering object properties based on value
(8 answers)
Closed 1 year ago.
I've a problem getting object keys by value for example:
issues: {
window_expired: 'Yes',
no_power: 'No',
no_display: 'Yes'
}
I want to get object keys which have 'Yes' Value as: window_expired, no_display
I've tried this method which is working but it's returning the 1st key i want to loop through and get the keys by value:
function getKeyByValue(object, value) {
return Object.keys(object).find((key) => object[key] === value);
}
you should use filter in your case.
Object.keys(issues).filter(e => issues[e] === 'Yes')
Have a good day
Related
This question already has answers here:
How to determine equality for two JavaScript objects?
(82 answers)
Object comparison in JavaScript [duplicate]
(10 answers)
Closed last year.
I want to compare two objects as below:
First object:
const permissions = {
"statistics":{"list":"1"},"audit":{"list":"1"}}
}
Second object:
const userPermission =
{
"audit":{"list":"1"}
}
So, if the second object has some value the same as the first object then return true.
In this sample I want it return true. Becuase, audit has same properties.
var obj1 = {
name:'hello',
age:12,
fav: 'fav',
foo: 'foo'
}
var obj2 = {
name: 'hey',
say: 'say',
prop: 'prop',
top: 'top'
}
var common = Object.keys(obj1).filter(obj1item => Object.keys(obj2).indexOf(obj1item) !== -1 );
console.log(common);
You can look into this > Is there a way to check and see if two objects have properties in common?
This question already has answers here:
How can I check if an object is an array? [duplicate]
(51 answers)
Closed 3 years ago.
I have the following variables which are arrays:
const gumBrands = ['orbit', 'trident', 'chiclet', 'strident'];
const mintBrands = ['altoids', 'certs', 'breath savers', 'tic tac'];
Below I have the following function that uses the variables as input arguments:
function shallowCopy (arrOne, arrTwo) {
if (arrOne.constructor === 'Array'){
return [...arrOne, ...arrTwo];
}
else {
console.log('test this');
}
}
shallowCopy(gumBrands, mintBrands)
I am expecting my code to return:
[ 'orbit',
'trident',
'chiclet',
'strident',
'altoids',
'certs',
'breath savers',
'tic tac' ]
Instead the code runs my else statement and returns: test this
What am I doing wrong?
.constructor does not contain the string "Array" but a reference to the global Array object.
Note that arrays can be subclassed, and their .constructor is different. You might want to consider to check with instanceof Array.
This question already has answers here:
Remove array element based on object property
(12 answers)
Closed 5 years ago.
I have an array, and I want to delete elements that have status equal to true (as seen in this part of code)
listArray.forEach((element, index) => {
if (element.status === true) {
listArray.splice(index, 1);
}
});
The problem is that, if, for example, the first, second and third elements have status true, than the second element will not be deleted
Try this:
listArray.filter(element => element.status === false)
You can also do:
listArray.filter(element => !element.status)
try this:
listArray = listArray.filter(element => !element.status);
This question already has answers here:
How to get a key in a JavaScript object by its value?
(31 answers)
Closed 5 years ago.
var json={"america":"1234","india:"5678","britain":"789"}
This is a sample json object (there are thousands of key-value pairs in the actual json object). How do I retrieve the key from this object if I pass the value json["1234"]? The expected result is that I should be able to retrieve the key "america".
var json = {"america":"1234","india":"5678","britain":"789"};
function getValueByKey(obj, val) {
// Looping on object keys
return Object.keys(obj).filter(function (key) {
return obj[key] === val
})[0];
}
// For testing
console.log(getValueByKey(json, "1234")); // america
console.log(getValueByKey(json, "5678")); // india
This question already has answers here:
Closed 11 years ago.
Possible Duplicates:
Javascript - array.contains(obj)
Best way to find an item in a JavaScript Array ?
I want to check, for example, for the word "the" in a list or map. Is there is any kind of built in function for this?
In javascript you have Arrays (lists) and Objects (maps).
The literal versions of them look like this:
var mylist = [1,2,3]; // array
var mymap = { car: 'porche', hp: 300, seats: 2 }; // object
if you which to figure out if a value exists in an array, just loop over it:
for(var i=0,len=mylist.length;i<len;i++) {
if(mylist[i] == 2) {
//2 exists
break;
}
}
if you which to figure out if a map has a certain key or if it has a key with a certain value, all you have to do is access it like so:
if(mymap.seats !== undefined) {
//the key 'seats' exists in the object
}
if(mymap.seats == 2) {
//the key 'seats' exists in the object and has the value 2
}
Array.indexOf(element) returns -1 if element is not found, otherwise returns its index