Get data from JSON by using a variable JavaScript/jQuery [duplicate] - javascript

This question already has answers here:
How can I access and process nested objects, arrays, or JSON?
(31 answers)
How to get values from JS object
(2 answers)
Closed 3 years ago.
I'm trying to get data out of a JSON using a value.
I have an array of objects:
"fruits": [
{
"Name": "Orange",
"Quantity": "5"
},
{
"Name": "Apple",
"Quantity": "2"
}
]
And I also have the name "Apple". How can I get the quantity of Apples?
I'm stuck on this..
Thanks!

fruits.find(fruit => fruit.Name==="Apple").Quantity is what you are looking for:
const fruits = [
{
"Name": "Orange",
"Quantity": "5"
},
{
"Name": "Apple",
"Quantity": "2"
}
]
console.log(fruits.find(fruit => fruit.Name==="Apple").Quantity);

Using a forEach you can check each element and if the Name matches get the Quantity
var fruits = [{
"Name": "Orange",
"Quantity": "5"
},
{
"Name": "Apple",
"Quantity": "2"
}
]
fruits.forEach(function(element) {
if (element.Name === 'Apple') {
console.log(element.Quantity)
}
});

Related

Sort object of objects to obtain the sorted result in object format only [duplicate]

This question already has answers here:
Sort array of objects by string property value
(57 answers)
Does JavaScript guarantee object property order?
(13 answers)
Closed 4 months ago.
I have an object like this :
{
"25276234": {
"id": "25276234",
"title": "new",
},
"4c57f8b9": {
"id": "4c57f8b9",
"title": "aa",
},
"c5353527": {
"id": "c5353527",
"title": "1",
},
"76517feb": {
"id": "76517feb",
"title": "bs",
},
"b6458d0c": {
"id": "b6458d0c",
"title": "new doc",
},
"d8ca47f7": {
"id": "d8ca47f7",
"title": "AAAA",
},
"2a72cba8": {
"id": "2a72cba8",
"title": "abcd",
},
"73694f31": {
"id": "73694f31",
"title": "A",
}
}
My aim is to sort it based on the title and the final sorted result should be an object of this format only and not an array.
I have tried several ways like -
const sorted = {};
Object
.keys(data).sort((o1,o2) => {
return data[o1].title - data[o2].title;
})
.forEach((key) => {
sorted[key] = data[key];
});
But it is not being sorted for some reason.
What am I doing wrong and what is the solution?

How can I iterate an array of arrays and keep only values from a single index in javascript? [duplicate]

This question already has answers here:
From an array of objects, extract value of a property as array
(24 answers)
Closed 8 months ago.
I am building a booking calendar using easepicker and importing data using a third-party API in a mySQL db. I got the data in the database and I can access it in Javascript (to which I'm new). I'm trying to remove all indexes but one from an array of arrays, but no matter how I try to do the loop I get a .pop() undefined, although I made sure the object is an array of arrays.
$.ajax({
url : 'get_data.php', // this fetches the booking info from 3rd party => DB
type : 'GET', // type of the HTTP request
success : checkAvail,
});
function checkAvail(response) {
results = response;
var results = jQuery.parseJSON(response);
availables = results.filter(a => a.isAvailable === '1');
console.log(availables); // displays the array of arrays for avail dates
availables.forEach((available) => {
available.pop(2); // **Uncaught TypeError: available.pop is not a function
});
console.table(availables);
}
console.log(availables) output
[
{
"id": "79653836",
"date": "2022-07-09",
"isAvailable": "1",
"price": "2188"
},
{
"id": "79653838",
"date": "2022-07-10",
"isAvailable": "1",
"price": "1750"
},
{
"id": "79653840",
"date": "2022-07-11",
"isAvailable": "1",
"price": "1750"
},
{
"id": "79653842",
"date": "2022-07-12",
"isAvailable": "1",
"price": "1750"
}
]
availables is an array of objects, not an array of arrays.
To get a particular property from an object, use .propname, so use available.date to get the date.
You can use map() to return a new array that uses this to get the properties.
const availables = [
{
"id": "79653836",
"date": "2022-07-09",
"isAvailable": "1",
"price": "2188"
},
{
"id": "79653838",
"date": "2022-07-10",
"isAvailable": "1",
"price": "1750"
},
{
"id": "79653840",
"date": "2022-07-11",
"isAvailable": "1",
"price": "1750"
},
{
"id": "79653842",
"date": "2022-07-12",
"isAvailable": "1",
"price": "1750"
}
];
const dates = availables.map(a => a.date);
console.log(dates);

How to filter from an array of objects one value and reduce it to one result per value without duplicates [duplicate]

This question already has answers here:
Get all unique values in a JavaScript array (remove duplicates)
(91 answers)
Remove duplicates form an array
(17 answers)
Closed 1 year ago.
I'm trying to build a method in javascript which can filter from an array of object one only value and have a result that value only once without having duplicates
sharing a snippet and more details what I want to achieve
const arr = [{
"id": "1",
"value": "something"
}, {
"id": "1",
"value": "something"
}, {
"id": "2",
"value": "something"
}, {
"id": "2",
"value": "something"
},
{
"id": "3",
"value": "something"
},
{
"id": "3",
"value": "something"
},
{
"id": "3",
"value": "something"
}];
const result = arr.filter(res => res.id).map(ele => ele.id);
console.log(result);
As you see result is an array like this
["1", "1", "2", "2", "3", "3", "3"]
What I would like to get is as follow
["1", "2", "3"]
The idea is to extract only one ID per result.
You can use a Set as follows:
const arr = [
{ "id": "1", "value": "something" },
{ "id": "1", "value": "something" },
{ "id": "2", "value": "something" },
{ "id": "2", "value": "something" },
{ "id": "3", "value": "something" },
{ "id": "3", "value": "something" },
{ "id": "3", "value": "something" }
];
const result = [...arr.reduce((set,{id}) => {
set.add(id);
return set;
}, new Set)];
console.log(result);

Iterate array of objects with string array [duplicate]

This question already has answers here:
Filter array of objects based on another array in javascript
(10 answers)
Closed 2 years ago.
I have an array of objects (JAVASCRIPT) like below
[
{
"code": "123",
"label": "Test123"
},
{
"code": "234",
"label": "Test"
},
{
"code": "980",
"label": "joe"
}
]
And i have a string array like below
["123", "234"]
I want to loop through array of objects and pass string array to get the "label"
I am expecting an output like below
[
{
"code": "123",
"label": "Test123"
},
{
"code": "234",
"label": "Test"
}
]
Please let me know if there is any efficient solution (JAVASCRIPT) because my array of objects is big.
Try this:
const obj = [
{
"code": "123",
"label": "Test123"
},
{
"code": "234",
"label": "Test"
},
{
"code": "980",
"label": "joe"
}
];
const arr = ["123", "234"];
var output = arr.flatMap(item => obj.filter(x => x.code == item));
console.log(output);
If the array is big, this can help to use Array.reduce.
const input = [{
"code": "123",
"label": "Test123"
},
{
"code": "234",
"label": "Test"
},
{
"code": "980",
"label": "joe"
}
]
const input2 = ["123", "234"];
const inputObj =input.reduce((acc, cur) => {
acc[cur.code] = cur.label;
return acc;
}, {});
const result = input2.reduce((acc, cur) => {
if (inputObj[cur]) {
acc.push({
code: cur,
label: inputObj[cur]
});
}
return acc;
}, []);
console.log(result);

Iterate through javascript nested objects [duplicate]

This question already has answers here:
How can I access and process nested objects, arrays, or JSON?
(31 answers)
Closed 2 years ago.
I have following json object :
"records": {
"abc": {
"id": "1",
"code": "A",
"iId": "2",
},
"xyz": {
"id": "1",
"code": "B",
"iId": "2",
}
}
I want to get code values and insert them into a new array. How can I achieve it ?
Thanks in advance
Let's try code below:
let obj = {
"records": {
"abc": {
"id": "1",
"code": "A",
"iId": "2",
},
"xyz": {
"id": "1",
"code": "B",
"iId": "2",
}
}
};
let arr = [];
for (let key in obj) {
for (let subkey in obj[key]){
arr.push(obj[key][subkey]['code']);
}
}
console.log(arr);

Categories

Resources