Find object by condition [duplicate] - javascript

This question already has answers here:
How to search JSON tree with jQuery
(11 answers)
Closed 5 years ago.
I have the following object "list":
{
...
2: {id: 35, name: 'dog Sharik'},
3: {id: 36, name: 'cat Murzik'}
...
}
Need to find object which consist 'cat' word in name.
What is the best way to do it (does Jquery can help with it?)

Iterate the loop and find it.Use a for loop to iterate the obj and return when the name property of the object contains the word cat.Here obj is your original object
CODE
function findmatching(obj,word){
for(var key in obj){
if(obj[key]['name'].indexOf(word)!=-1){
return obj[key]
}
}
}
var mymatchingvalue=findmatching(userinput,word)

You can use lodash/underscore.js
_.filter(list, function(d) { return _.includes(d.name, 'cat') })

Related

How to display object values using for/in loop in Javascript [duplicate]

This question already has answers here:
How do I loop through or enumerate a JavaScript object?
(48 answers)
Closed 1 year ago.
I have written a for/in loop which displays the properties for the object person here:
const composer = {
name: 'Edward Ellington',
nickname: 'Duke',
genres: ['jazz', 'swing'],
instrument: 'piano'
};
for (let prop in composer) {
console.log(`${prop}: ${composer[prop]}`);
}
My question is how do I now display the values of the object? I apologize if this has already been answered but I did not find this question in my search.
I have tried to add this below console.log:
console.log(composer.values(object1));
But it tells me this is wrong. What am I missing?
You want Object.values.
const composer = {
name: 'Edward Ellington',
nickname: 'Duke',
genres: ['jazz', 'swing'],
instrument: 'piano'
};
const values = Object.values(composer);
console.log(values);
Can't see your code. I think what you are asking is can be achieved as follows:
for (let key in obj) {
// value is accessed with this notation `obj[key]`
console.log(obj[key]);
}

How to get data and create an array from an object when keys are same in javascript? [duplicate]

This question already has answers here:
Javascript object literal - possible to add duplicate keys?
(3 answers)
Closed 1 year ago.
This is the scenario I am talking about:
let obj = {
id: "kjhgfr^&*()(*UY",
id: "kjhgfr^OIJHB",
id: "kjhgfr^)(*&^%Y",
id: "DFGHI(*&YTRDTYHKI*",
id: ")(IUHGVYUJKO))(*UY",
id: "VGYUKO(*UYH",
id: "BHYUIOP)(*&^T%",
id: "0987654567890",
id: "5678909876543",
};
I want to create an array with ids like this.
[
"5678909876543",
"0987654567890",
"VGYUKO(*UYH",
"kjhgfr^&*()(*UY",
"VGYUKO(*UYH",
];
Your object is incorrect.
Possible it is the same as:
Javascript object literal - possible to add duplicate keys?
As a result, it will be override to get the last value.
let obj ={id:"kjhgfr^&*()(*UY",id:"kjhgfr^OIJHB",id:"kjhgfr^)(*&^%Y",id:"DFGHI(*&YTRDTYHKI*",id:")(IUHGVYUJKO))(*UY",id:"VGYUKO(*UYH",id:"BHYUIOP)(*&^T%",id:"0987654567890",id:"5678909876543",};
console.log(obj);
// { "id": "5678909876543"}
You cannot have duplicate keys. Each identical key will overwrite the previously defined value.
You could try this instead (assuming you have control over the input):
let obj = {
id: ["kjhgfr^&*()(*UY",
"kjhgfr^OIJHB",
"kjhgfr^)(*&^%Y",
"DFGHI(*&YTRDTYHKI*",
")(IUHGVYUJKO))(*UY",
"VGYUKO(*UYH",
"BHYUIOP)(*&^T%",
"0987654567890",
"5678909876543"]
};
This is similar to the following question: Read and loop through an object with non-unique key value pairs

I am getting this type of array when i try to push object into an array in jquery [duplicate]

This question already has answers here:
What is the scope of variables in JavaScript?
(27 answers)
Closed 3 years ago.
while inspecting in browser, I am getting this type of array and I am unable to access 0th index of array.
function xyz()
{
var obj={
name: "abc",
age: "20"
var VArr=[];
VArr.push(obj);
}
[]
0:Array(1)
0:{name:"abc",age:"20"}
First acess to the array by VArr[0] inside this u have your objecet to access your object property use dot notation like this
console.log(VArr[0].name)
Full code
function xyz(){
var obj={
name: "abc",
age: "20"
}
var VArr=[];
VArr.push(obj);
console.log(VArr[0].name)
}
xyz()

How you do search all object values inside an array? [duplicate]

This question already has answers here:
Find object by id in an array of JavaScript objects
(36 answers)
Closed 4 years ago.
How do I search all values inside all objects in an array object in javascript?
const array = [
{
id: 145,
name: "Test"
},
{
id: 241,
name: "Array"
}
]
if I run a function it returns find('a') it returns the second object or if i run find(1) it returns both of the object
something like this
var matches = [];
for(i=0;i<array.length;i++)
{
if(array[i].name.indexOf('a') > -1)
{
matches.Push(array[i]);
}
}
return matches;
This isn't valid javascript as such, but that is how you would write something like this in javascript.

How to get number of nested objects in object? [duplicate]

This question already has answers here:
Length of a JavaScript object
(43 answers)
Closed 9 years ago.
I have an Object. How to get number of nested objects in a?
a = {
firstNested: {
name: 'George'
}
secondNested: {
name: 'James'
}
}
I supposed to use .length which is usually used for arrays. What should I do in this case?
Yes, duplicate of above.. Just use:
var a = {1:2, 3:4},
count = 0,
item;
for(item in a) {
count++;
}
alert(count);

Categories