Getting the object from an array on the basis of id [duplicate] - javascript

This question already has answers here:
How to find an appropriate object in array by one of its properties
(3 answers)
Closed 29 days ago.
I have a array as follows :
data = [
{
"data": {
"id":1,
"vol":"0.0"
"requiredId":100
"details":{
"ABC":"8.30",
"OFG":"13.85",
"SPG":"70.80"
}
}
},
{
"data": {
"id":2,
"vol":"1.0",
"requiredId":2
"details":{
"ABC":"3.30",
"OFG":"15.85",
"SPG":"70.80"
}
}
}
]
I just want to get the object from this array where id is equal to requiredId. How can I do that?

data.find(item => item.data.id === item.data.requiredId)

Related

Javascript proper way to .push array [duplicate]

This question already has an answer here:
javascript push returning number instead of object [duplicate]
(1 answer)
Closed 3 months ago.
How to use .push array in proper way using javascript?
my code
let persons = [{
"person1": "person1"
},
{
"person1": "person1"
}
]
let addPerson = []
addPersn.push({
"person1": "person2"
})
let allPerson = persons.push(addPerson)
console.log(allPerson)
expected behavior
{
"person1": "person1"
},
{
"person1": "person1"
},
{
"person1": "person1"
}
current result
3
How to use .push array in proper way using javascript?How to use .push array in proper way using javascript?
Try this
let persons = [
{
person1: "person1",
},
{
person2: "person2",
},
];
const newPerson = {
person3: "person3",
}
persons.push(newPerson);
console.log(persons);

Remove empty Object from JSON arrays [duplicate]

This question already has answers here:
Remove Object From Array if the value is empty in name and value pair js
(3 answers)
Closed 2 years ago.
I am trying to remove this empty key value pair object from this JSON array but it I am unable to figure out on how to do it.
I tried this:
var array = {
"title":[
{"lang":"English","value":"yes"},
{"lang":"Spanish","value":"no"},
{"lang":"German","value":""}
]
}
var result = array['title'].filter(function(x){return x.length});
This gives the following output:
{
"title":[
{"lang":"English","value":"yes"},
{"lang":"Spanish","value":"no"},
{"lang":"German"}
]
}
Output expected:
{
"title":[
{"lang":"English","value":"yes"},
{"lang":"Spanish","value":"no"}
]
}
Change your filter to look for an empty string in the value property:
var array = {
"title": [{
"lang": "English",
"value": "yes"
},
{
"lang": "Spanish",
"value": "no"
}, {
"lang": "German",
"value": ""
}
]
}
var result = array['title'].filter(x=>x.value !== "");
console.log(result);
You should do something like this.
array['title'].filter( element => element.value )
You aren't specifying what it's looking for for each element.
Try changing your filter function to check if the value exists and has a length greater than 0:
function(x) { return x.value && x.value.length > 0 }

Converting a JSON object to array? Read description [duplicate]

This question already has answers here:
Converting a JS object to an array using jQuery
(18 answers)
Closed 5 years ago.
my server upon request, serves JSON from a node mysql query, but only the names row. Example:
{
"Name": "Charles"
}, etc.
How can I, get the value of Name and put it into an array? say I have this,
[
{
"Name": "Charles"
},
{
"Name": "Alex"
}
]
how can I get, Charles and Alex, into an array?
Like:
Names = ["Charles", "Alex]?
You can make use of the map function. The map method creates a new array with the results of calling a provided function on every element in this array. In your case, you need to select only the Name.
var arr = [
{
"Name": "Charles"
},
{
"Name": "Alex"
}];
var names = arr.map(x=>x.Name)
console.log(names);

How to iterate JSON objects with JSON objects in every object (JavaScript)? [duplicate]

This question already has answers here:
How can I access and process nested objects, arrays, or JSON?
(31 answers)
Closed 6 years ago.
I have multiple JSON objects which are have multiple JSON objects.
Now how do i iterate the Objects in deep.
My JSON structure is like below.
{
"phanim": {
"msg1": {
"data": "Hii ra..",
"date": "Sep 9",
"sender": "Phani",
"type": "text"
},
"msg2": {
"data": "How r u?",
"date": "Sep 9",
"sender": "Phani",
"type": "text"
}
}
}
Probably a recursive function - check if the typeof the current iterated property is an object, if so, iterate that!
var iterateObj = function(obj) {
for (var key in obj) {
if (typeof obj[key] === "object") {
iterateObj(obj[key]);
} else {
console.log(obj[key]);
}
}
}
Note: this won't work if one of your properties is an array - you'll need an additional check and logic for that.

How to query json data nodes using JavaScript [duplicate]

This question already has answers here:
How can I access and process nested objects, arrays, or JSON?
(31 answers)
Find object by id in an array of JavaScript objects
(36 answers)
Closed 9 years ago.
I wish to query json result data to be used in Razor view and would like to know how to search by a specific id and capture data for a specific node:
Here is an example(just a part) of my json:
{
"maxPages":1,
"data":[
{
"id":"123",
"perfomanceData":[
{
"platform":{
"name":"Test0",
"id":0
},
],
{
"id":"124",
"perfomanceData":[
{
"platform":{
"name":"Test1",
"id":0
},
],
{
"id":"125",
"perfomanceData":[
{
"platform":{
"name":"Test2",
"id":0
},
],
I wish to search by an ID example 125 and retrieve just this section
perfomanceData":[
{
"platform":{
"name":"Test2",
"id":0
},
]
How can I use JavaScript to do the above?

Categories