This question already has answers here:
Find object by id in an array of JavaScript objects
(36 answers)
Closed 4 years ago.
when i am using fetch i am getting data in json format. I want to search json via cust_id and return only matching record. please tell me how to do it. I have to show all the credit card the person have.
"objects": [
{
"card_number": "123412341234",
"created_at": "2018-10-06T06:02:25.053942",
"cust_id": "12345",
"id": 1,
"resource_uri": "/api/card/1/",
"status": "completed"
},
{
"card_number": "213412345678",
"created_at": "2018-10-06T06:53:22.588967",
"cust_id": "12345",
"id": 3,
"resource_uri": "/api/card/3/",
"status": "pending"
},
]
var objects= [
{
"card_number": "123412341234",
"created_at": "2018-10-06T06:02:25.053942",
"cust_id": "12345",
"id": 1,
"resource_uri": "/api/card/1/",
"status": "completed"
},
{
"card_number": "213412345678",
"created_at": "2018-10-06T06:53:22.588967",
"cust_id": "12345",
"id": 3,
"resource_uri": "/api/card/3/",
"status": "pending"
},
{
"card_number": "213412345678",
"created_at": "2018-10-06T06:53:22.588967",
"cust_id": "12345",
"id": 3,
"resource_uri": "/api/card/3/",
"status": "pending"
},
]
console.log(objects.filter(i=>i['cust_id'] === '12345'))
I hope it helps
var val={
"objects": [
{
"card_number": "123412341234",
"created_at": "2018-10-06T06:02:25.053942",
"cust_id": "12345",
"id": 1,
"resource_uri": "/api/card/1/",
"status": "completed"
},
{
"card_number": "213412345678",
"created_at": "2018-10-06T06:53:22.588967",
"cust_id": "12345",
"id": 3,
"resource_uri": "/api/card/3/",
"status": "pending"
},
]
};
function get(val,custid){
var result;
for(var i=0;i<val.objects.length;i++){
if(val.objects[i].cust_id=custid){
result= val.objects[i];
}
}
return result;
}
console.log(get(val,"1235"));
For multiple records you can take result as array and use array.push to insert in result (function variable) and return the array in end
Related
I want to reference my Strapi database to my Next.JS Frontend. How can I reference a JSON data if it is also an object within the object?
As an example, to access the name, I use {activity.name}. How can I access the providerName?
I tried {activity.provider.providerName} but it is not working. What am I doing wrong?
[
{
"id": 1,
"name": "Bowling in Mundsburg",
"activityType": "Bowling",
"profileType": "Basic",
"adventureType": "Activity",
"slug": "bowling-in-new-york",
"provider": {
"id": 1,
"providerName": "Bowlingcenter New York",
"slug": "bowlingcenter-new-york",
"published_at": "2021-09-26T12:06:12.670Z",
"created_at": "2021-09-26T12:04:28.585Z",
"updated_at": "2021-09-26T12:06:12.690Z"
},
...
]
could you try like so?
const strapiRes = [
{
"id": 1,
"name": "Bowling in Mundsburg",
"activityType": "Bowling",
"profileType": "Basic",
"adventureType": "Activity",
"slug": "bowling-in-new-york",
"provider": {
"id": 1,
"providerName": "Bowlingcenter New York",
"slug": "bowlingcenter-new-york",
"published_at": "2021-09-26T12:06:12.670Z",
"created_at": "2021-09-26T12:04:28.585Z",
"updated_at": "2021-09-26T12:06:12.690Z"
}
}
];
const whatYouAreAskingFor = strapiRes[0].provider.providerName
also if you are looping try something like:
strapiRes.map(activity => activity.provider.providerName)
I am trying to set a variable from following phone number with value: “+33652556777” (index 4 in JSON attached below) which is the last object in contacts (index 4).
To do so is pretty simple:
let jsonData = pm.response.json();
console.log (jsonData.contacts[4].phone_numbers[0].value)
const PhoneNumber = jsonData.contacts[4].phone_numbers[0].value
pm.environment.set("Jacky", PhoneNumber);
Since I have to use different query parameters to filter by eg. created_at=asc, desc, the property of the phone_numbers order might change index number and I won’t be able to fetch desire phone number "+33652556777” instead it will set a different phone number which I cannot allow.
I know there is way to fetch our number and make it variable for next requests, which is iterating over properties or keys in the object “ for….in or for…of ” but for some reason I cannot achieve it.
What I could achieve is to get through first object “contacts” but impossible to get to its nested array “phone_numbers”. Here is how I did it:
let jsonData = pm.response.json();
let contact;
for (let filter of jsonData.contacts){
if (filter.last_name == "Rowland"){
contact = filter;
}}
console.log (contact);
Could you please help?
Here goes the JSON body response:
{
"contacts": [
{
"id": 11121211,
"direct_link": "https://example.example",
"first_name": "test1",
"last_name": "test",
"company_name": "test",
"information": null,
"is_shared": true,
"created_at": 1582798926,
"updated_at": 1582798926,
"emails": [],
"phone_numbers": [
{
"id": 60065270,
"label": "Work",
"value": "+33134567666"
}
]
},
{
"id": 22222222,
"direct_link": "https://example.example",
"first_name": null,
"last_name": null,
"company_name": null,
"information": null,
"is_shared": true,
"created_at": 1583686067,
"updated_at": 1583686067,
"emails": [],
"phone_numbers": [
{
"id": 22266444,
"label": "Work",
"value": "+33134567899"
}
]
},
{
"id": 33333564,
"direct_link": "https://example.example",
"first_name": "Jessica",
"last_name": "Biel",
"company_name": "N-Sync",
"information": null,
"is_shared": true,
"created_at": 1583686086,
"updated_at": 1583686086,
"emails": [],
"phone_numbers": []
},
{
"id": 45678901,
"direct_link": "https://example.example",
"first_name": null,
"last_name": null,
"company_name": null,
"information": null,
"is_shared": true,
"created_at": 1583686105,
"updated_at": 1583686105,
"emails": [],
"phone_numbers": [
{
"id": 22266444,
"label": "Work",
"value": "+33134567333"
}
]
},
{
"id": 56789123,
"direct_link": "https://example.example",
"first_name": "Jacky",
"last_name": "Rowland",
"company_name": "Test Company1",
"information": "",
"is_shared": true,
"created_at": 1583745888,
"updated_at": 1608556499,
"emails": [
{
"id": 76594398,
"label": "Work",
"value": "mandatory_field#example.com"
}
],
"phone_numbers": [
{
"id": 60650277,
"label": "Mobile",
"value": "+33652556777"
}
]
}
],
"meta": {
"count": 6,
"total": 241,
"current_page": 1,
"per_page": 5,
"next_page_link": "https://example.example",
"previous_page_link": null
}
}
You could use something basic like this:
_.each(pm.response.json().contacts, (contact) => {
if(contact.last_name === "Rowland") {
pm.environment.set(`${contact.first_name}_${contact.last_name}_number`, contact.phone_numbers[0].value)
}
})
There are probably better and more performant ways to do this but if you just want to set a variable for that contact, no matter where they are in the response - This would work :D
you can use forEach or _.each as danny mentioned to get all numbers else use:
console.info(jsonData.contacts.find((a)=>a.first_name==="Jacky").phone_numbers[0].value)
use array.find to find the contact with first_name jacky adn then get phone_number[0].value from it.
if you want all numbers from that array then use:
console.info(jsonData.contacts.find((a)=>a.first_name==="Jacky").phone_numbers.map((a)=>a.value))
here we map the result to get only the numbers from phone_number array.
is it what you looking for !?
I'm trying to remove an object from the array, even though, I'm not sure how to grab the object, which basically makes the code that is written not make anything.
Let me show the existing array:
Object {
"created_at": "2020-06-30T11:22:53.000000Z",
"icon": "subjectImgs/509-1080x1080.jpg",
"id": 2,
"name": "Test",
"updated_at": "2020-06-30T11:22:53.000000Z",
"user_id": 1,
},
Object {
"created_at": "2020-06-04T18:32:25.000000Z",
"icon": "subjectImgs/698-1080x1080.jpg",
"id": 1,
"name": "history",
"updated_at": "2020-06-04T18:32:25.000000Z",
"user_id": 1,
},
Object {
"created_at": "2020-07-08T16:32:03.000000Z",
"icon": "subjectImgs/698-1080x1080.jpg",
"id": 21, // Here is the ID
"name": "123",
"updated_at": "2020-07-08T16:32:03.000000Z",
"user_id": 1,
},
}
When the event is listened to, I get the array of the object that was just deleted:
Object {
"socket": null,
"subject": Object {
"created_at": "2020-07-08T16:32:03.000000Z",
"icon": "subjectImgs/698-1080x1080.jpg",
"id": 21, // Here is the ID
"name": "123",
"updated_at": "2020-07-08T16:32:03.000000Z",
"user_id": 1,
},
}
Now, the way I'm trying to delete it is the following:
.listen('SubjectRemoved', ev => {
var array = this.state.subjects
this.setState({ subjects: array.filter((id) => id != ev.subject.id) })
});
but apparently, this is not working. I assume because of using id really gets nothing.
I think that the problem is in the filter method:
array.filter((id) => id != ev.subject.id) // here id is an elemt of the array not the real id
You could try
array.filter((subject) => subject.id != ev.subject.id) //if is an array of subjects
or
array.filter((elem) => elem.subject.id != ev.subject.id) //if is an array of objects like {socket: null, subject: {... with the fields }}
If you think that the ev.subject.id is not working you could just console.log it before the filter senetence.
I have a json object with array of data.i want to filter it with another array.
MY .ts file is as follows.
let filter_bank_id=[4,25,6,1];
console.log(filter_bank_id.length);
if(data.offers){
let last =data.offers.filter(offer=>{
for(let i=0;i<filter_bank_id.length;i++){
if(data.offers[i]){
let bank=filter_bank_id[i];
if(bank){
if(bank===data.offers[i].bank_id){
return offer;
}
}
}else{
alert("nodata");
}
}
});
console.log(last);
}
Here offers is the json object with multiple data.i want to filter it with filter_bank_id array.That means i want only offers with bank_id 4,25,6,1 as in the filter_bank_id array.But it is not working.
problem is with " let last =data.offers.filter(offer=>" this line.when i give debugger it is not entering into it.My offers json object is below.
offers= [
{
"image": "assets\/banks\/axi1419231043.jpg",
"offerid": 1,
"id": "1",
"bank_id": "1",
"name": "AXIS BANK",
"bank_interest": "10.99",
"emi": "2,174",
"processing_fee": "990",
"precloser_cost": "0 %",
"part_pay": "Yes",
"insurance": null,
"conditions": "",
"intrest_1_year": 0,
"intrest_total": 0
},
{
"image": "assets\/banks\/hdfc1418896652.png",
"offerid": 7,
"id": "4",
"bank_id": "4",
"name": "HDFC BANK",
"bank_interest": "10.99",
"emi": "2,174",
"processing_fee": "500",
"precloser_cost": "4.49 % for 12-24 months,3.37 % for 24-36 months,2.25 % for 36-60 months,",
"part_pay": "Yes",
"insurance": "1,362",
"conditions": "",
"intrest_1_year": 0,
"intrest_total": 0
},
{
"image": "assets\/banks\/scb1438520764.png",
"offerid": 2,
"id": "16",
"bank_id": "16",
"name": "SCB",
"bank_interest": "11.00",
"emi": "2,175",
"processing_fee": "1000",
"precloser_cost": "0 %",
"part_pay": "Yes",
"insurance": null,
"conditions": "",
"intrest_1_year": 0,
"intrest_total": 0
},
{
"image": "assets\/banks\/citi1419219218.png",
"offerid": 3,
"id": "2",
"bank_id": "2",
"name": "CITI BANK",
"bank_interest": "11.49",
"emi": "2,199",
"processing_fee": "2999",
"precloser_cost": "2 %",
"part_pay": "Yes",
"insurance": null,
"conditions": "",
"intrest_1_year": 0,
"intrest_total": 0
},
];
What is the problem with my filter.please help me.Thank in advance.
Use filter like this
offers.filter((offer) => filter_bank_ids.indexOf(offer.bank_id) > -1)
Also check your types before using this snippet. I hope this will help you
I have a nested JSON returned from an API that I am hitting using a GET request, in POSTMAN chrome app. My JSON looks like this
"result": [
{
"_id": "some_id",
"name": "India",
"code": "IN",
"link": "http://www.india.info/",
"closingTime": "2017-02-25T01:12:17.860Z",
"openingTime": "2017-02-25T06:12:17.205Z",
"image": "image_link",
"status": "online",
"serverStatus": "online",
"games": [
{
"_id": "some_game_id1",
"name": "Cricket"
},
{
"_id": "some_another_id1",
"name": "Baseball"
},
{
"_id": "some_another_id_2",
"name": "Basketball"
}
]
},
{
"_id": "some_id",
"name": "Australia",
"code": "AUS",
"link": "https://www.lonelyplanet.com/aus/adelaide",
"closingTime": "2017-02-28T05:13:38.022Z",
"openingTime": "2017-02-28T05:13:38.682Z",
"image": "some_image_url",
"status": "offline",
"serverStatus": "online",
"games": [
{
"_id": "some_game_id_2",
"name": "Cricket"
},
{
"_id": "some_another_id_3",
"name": "Kho-Kho"
},
{
"_id": "some_another_id_4",
"name": "Badminton"
},
{
"_id": "some_another_id_5",
"name": "Tennis"
}
]
},
I am trying to test whether my response body has "name":"India" and the "game" with "some_game_id1" contains the "name":"cricket".
I went through this link where the answer is to have an array for "name"created and then check within the array whether the array contains the value. I tried this but my code fails.
Also, I tried searching the element by the index within the JSON body using this -
var searchJSON = JSON.parse(responseBody);
tests["name contains India"] = searchJSON.result.name[0]==="India";
But this also fails. I tried using the .value appended with the second line of above code, but it also fails. How can I check this thing?
You need to put [0] after result (which is an array) rather than name (which is a string).
Also, use a regular expression to check whether the name contains 'India', because using === only checks if the name is exactly India.
var searchJSON = JSON.parse(responseBody)
tests["name contains India"] = /India/.test(searchJSON.result[0].name)
Demo Snippet:
var responseBody = `{
"result": [{
"_id": "some_id",
"name": "India",
"code": "IN",
"link": "http://www.india.info/",
"closingTime": "2017-02-25T01:12:17.860Z",
"openingTime": "2017-02-25T06:12:17.205Z",
"image": "image_link",
"status": "online",
"serverStatus": "online",
"games": [{
"_id": "some_game_id1",
"name": "Cricket"
},
{
"_id": "some_another_id1",
"name": "Baseball"
},
{
"_id": "some_another_id_2",
"name": "Basketball"
}
]
},
{
"_id": "some_id",
"name": "Australia",
"code": "AUS",
"link": "https://www.lonelyplanet.com/aus/adelaide",
"closingTime": "2017-02-28T05:13:38.022Z",
"openingTime": "2017-02-28T05:13:38.682Z",
"image": "some_image_url",
"status": "offline",
"serverStatus": "online",
"games": [{
"_id": "some_game_id_2",
"name": "Cricket"
},
{
"_id": "some_another_id_3",
"name": "Kho-Kho"
},
{
"_id": "some_another_id_4",
"name": "Badminton"
},
{
"_id": "some_another_id_5",
"name": "Tennis"
}
]
}
]
}`
var tests = {}
var searchJSON = JSON.parse(responseBody)
tests["name contains India"] = /India/.test(searchJSON.result[0].name)
console.log(tests) //=> { "name contains India": true }