This question already has answers here:
From an array of objects, extract value of a property as array
(24 answers)
Closed 3 years ago.
I Want to separate the elements from an array of objects in to another array
emplist = [
{
"empid": "CL4NX1569868029",
"orgid": "ZARQP1569826662",
"accid": "95057056798",
"fstname": "Testname",
"lastname": "Last",
"email": "test.mk#gmail.com"
},
{
"empid": "HMXEN1569860677",
"orgid": "ZARQP1569826662",
"accid": "9505705709",
"fstname": "Testname1",
"lastname": "Last",
"email": "test.mk#gmail.com"
},
{
"empid": "UX74A1569908006",
"orgid": "ZARQP1569826662",
"accid": "9100",
"fstname": "abc",
"lastname": "abc",
"email": "abc#abc.com"
},
]
I want to separate fstname and lastname like
source = [ "Testname Last" , "Testname1 Last" , "abc abc" ]
You can simply use map to transform the emplist array into another array with first and last names concatenated (here I used template literal, but you can just use +).
const emplist = [
{
"empid": "CL4NX1569868029",
"orgid": "ZARQP1569826662",
"accid": "95057056798",
"fstname": "Testname",
"lastname": "Last",
"email": "test.mk#gmail.com"
},
{
"empid": "HMXEN1569860677",
"orgid": "ZARQP1569826662",
"accid": "9505705709",
"fstname": "Testname1",
"lastname": "Last",
"email": "test.mk#gmail.com"
},
{
"empid": "UX74A1569908006",
"orgid": "ZARQP1569826662",
"accid": "9100",
"fstname": "abc",
"lastname": "abc",
"email": "abc#abc.com"
},
];
const source = emplist.map((emp) => `${emp.fstname} ${emp.lastname}`);
console.log(source);
Related
How can I loop through the data array of objects, and then join each guestList array into a single array to be used elsewhere?
const data =
[
{
"guestList": [
{
"firstName": "test",
"surname": "test",
"email": "test#test.com",
},
{
"firstName": "test",
"surname": "tesT",
"email": "test#test.com",
}
],
},
{
"guestList": [
{
"firstName": "test",
"surname": "test",
"email": "test#test.com",
},
{
"firstName": "test",
"surname": "tesT",
"email": "test#test.com",
}
],
},
{
"guestList": [
{
"firstName": "test",
"surname": "test",
"email": "test#test.com",
},
{
"firstName": "test",
"surname": "tesT",
"email": "test#test.com",
}
],
}
]
You can use flatMap and destructuring like this:
const data =[{"guestList": [{"firstName": "test","surname": "test","email": "test#test.com",},{"firstName": "test","surname": "tesT","email": "test#test.com",}],},{"guestList": [{"firstName": "test","surname": "test","email": "test#test.com",},{"firstName": "test","surname": "tesT","email": "test#test.com",}],},{"guestList": [{"firstName": "test","surname": "test","email": "test#test.com",},{"firstName": "test","surname": "tesT","email": "test#test.com",}],}];
const result = data.flatMap(({guestList}) => guestList);
console.log(result);
I have a response body that looks something like this:
{
"id": "provider-a7d49a99-53c0-4b7c-9be3-8b9efc828f1b",
"fullName": "Dr. Tim Lucca, FSE",
"firstName": "Tim",
"lastName": "Lucca",
"prefix": "Dr.",
"suffix": "FSE",
"phone": "(303) 520-4571",
"state": "CO",
"doseSpotID": 185012,
"active": "active",
"capabilities": {
"demographic": [
"adolescent",
"adult",
"child"
],
"geographic": [
"CO",
"TX"
],
"linguistic": [
"english",
"spanish"
],
"credentialedPayors": null
},
"invitePending": false
},
{
"id": "provider-450de310-fcb5-4e71-9d72-b6b320b2eb0a",
"fullName": "Mr Doc Torr",
"firstName": "Doc",
"lastName": "Torr",
"prefix": "Mr",
"suffix": "",
"phone": "(303) 520-4571",
"state": "CO",
"doseSpotID": 186856,
"active": "active",
"capabilities": {
"demographic": [
"adult",
"adolescent",
"child"
],
"geographic": [
"CO",
"TX"
],
"linguistic": [
"english"
],
"credentialedPayors": null
},
"invitePending": false
}
]
I have a search field for the user that they are able to search by fullName, phone, and geographic. Im trying to understand how to sort the list returned and display only the items in the list that have a match for the users input. What would be a good way to sort through the array of objects and creating an array of matches for the users search? Right now my component is just displaying all of the items returned.
for an exect match i would do this
let to_sort =
[
{
"id": "provider-a7d49a99-53c0-4b7c-9be3-8b9efc828f1b",
"fullName": "Dr. Tim Lucca, FSE",
"firstName": "Tim",
"lastName": "Lucca",
"prefix": "Dr.",
"suffix": "FSE",
"phone": "(303) 520-4571",
"state": "CO",
"doseSpotID": 185012,
"active": "active",
"capabilities": {
"demographic": [
"adolescent",
"adult",
"child"
],
"geographic": [
"CO",
"TX"
],
"linguistic": [
"english",
"spanish"
],
"credentialedPayors": null
},
"invitePending": false
},
{
"id": "provider-450de310-fcb5-4e71-9d72-b6b320b2eb0a",
"fullName": "Mr Doc Torr",
"firstName": "Doc",
"lastName": "Torr",
"prefix": "Mr",
"suffix": "",
"phone": "(303) 520-4571",
"state": "CO",
"doseSpotID": 186856,
"active": "active",
"capabilities": {
"demographic": [
"adult",
"adolescent",
"child"
],
"geographic": [
"CO",
"TX"
],
"linguistic": [
"english"
],
"credentialedPayors": null
},
"invitePending": false
}
]
let user_fullName = 'Dr. Tim Lucca, FSE'
let phone = '(303) 520-4571'
let geographic = 'CO'
let result = to_sort.filter((obj)=>{
if(obj.fullName !== user_fullName){
return false
}
if(obj.phone !== phone){
return false
}
if(!obj.capabilities.geographic.includes(geographic)){
return false
}
return true
});
to check if 1 of the properties is the same (not gonna put the data in is its quite big)
let user_fullName = 'Dr. Tim Lucca, FSE'
let phone = '(303) 520-4571'
let geographic = 'CO'
let result = to_sort.filter((obj)=>{
if(obj.fullName === user_fullName || obj.phone === phone || obj.capabilities.geographic.includes(geographic)){
return true
}
return false
});
in both cases result is an array of objects which have met the criteria
Here is my mvc code, I want to add multiple travelers objects to travelers array which are generated in a loop and then JSON.stringify them,
return amadeus.booking.flightOrders.post(
JSON.stringify({
'data':{
'type': 'flight-order',
'flightOffers': [response.data.flightOffers[0]],
'travelers':[{
"id": 1,
"name": {
"firstName": req.body.firstname,
"lastName": req.body.lastname
},
"gender": req.body.gender,
"contact": {
"emailAddress": req.body.emailaddress,
"phones": [{
"deviceType": req.body.devicetype,
"countryCallingCode": req.body.countrycallingcode,
"number": req.body.number
}]
},
"documents": [{
"documentType": req.body.documentype,
"birthPlace": req.body.birthplace,
"issuanceLocation": req.body.issuancelocation,
"issuanceDate": req.body.issuancedate,
"number": req.body.p_number,
"expiryDate": req.body.expirydate,
"issuanceCountry": req.body.issuancecountry,
"validityCountry": req.body.validitycountry,
"nationality": req.body.nationality,
"holder": true
}]
}]
}
})
);
I there a simple way to achieve that?
JSON.stringify converts a JS object to a string. It does not control or require any specifications for the object's structure in general.
In your case, you can simply add multiple data objects to your travelers' array e.g
return amadeus.booking.flightOrders.post(
JSON.stringify({
'data':{
'type': 'flight-order',
'flightOffers': [response.data.flightOffers[0]],
'travelers':[{
"id": 1,
"name": {
"firstName": req.body.firstname,
"lastName": req.body.lastname
},
"gender": req.body.gender,
"contact": {
"emailAddress": req.body.emailaddress,
"phones": [{
"deviceType": req.body.devicetype,
"countryCallingCode": req.body.countrycallingcode,
"number": req.body.number
}]
},
"documents": [{
"documentType": req.body.documentype,
"birthPlace": req.body.birthplace,
"issuanceLocation": req.body.issuancelocation,
"issuanceDate": req.body.issuancedate,
"number": req.body.p_number,
"expiryDate": req.body.expirydate,
"issuanceCountry": req.body.issuancecountry,
"validityCountry": req.body.validitycountry,
"nationality": req.body.nationality,
"holder": true
}]
},{
"id": 2,
"name": {
"firstName": req.body.firstname,
"lastName": req.body.lastname
},
"gender": req.body.gender,
"contact": {
"emailAddress": req.body.emailaddress,
"phones": [{
"deviceType": req.body.devicetype,
"countryCallingCode": req.body.countrycallingcode,
"number": req.body.number
}]
},
"documents": [{
"documentType": req.body.documentype,
"birthPlace": req.body.birthplace,
"issuanceLocation": req.body.issuancelocation,
"issuanceDate": req.body.issuancedate,
"number": req.body.p_number,
"expiryDate": req.body.expirydate,
"issuanceCountry": req.body.issuancecountry,
"validityCountry": req.body.validitycountry,
"nationality": req.body.nationality,
"holder": true
}]
},{
"id": 3,
.
.
.
},{
"id": 4,
.
.
.
}]
}
})
);
and JSON.stringify will convert whole object to string.
To loop on data and add the travelers array, one approach will be:
const travelers = [];
for(let i=0;i<10;i++){
travelers.push({});
}
and then
return amadeus.booking.flightOrders.post(
JSON.stringify({
'data':{
'type': 'flight-order',
'flightOffers': [response.data.flightOffers[0]],
'travelers': travelers
}
})
);
Im trying to filter and take only the contacts that the number is in numbers my problem is that one numbers could be the secondary number of the contact and filter(indexOf(foreach())) doesn't seem to work here any advice?
const filteredContacts = contacts.filter(contact => numbers.indexOf(contact.phoneNumbers.forEach(phone => phone.number)) > -1);
//sample of contacts
Object {
"company": "Financial Services Inc.",
"contactType": "person",
"firstName": "Hank",
"id": "2E73EE73-C03F-4D5F-B1E8-44E85A70F170",
"imageAvailable": false,
"jobTitle": "Portfolio Manager",
"lastName": "Zakroff",
"middleName": "M.",
"name": "Hank M. Zakroff",
"phoneNumbers": Array [
Object {
"countryCode": "us",
"digits": "5557664823",
"id": "337A78CC-C90A-46AF-8D4B-6CC43251AD1A",
"label": "work",
"number": "(555) 766-4823",
},
Object {
"countryCode": "us",
"digits": "7075551854",
"id": "E998F7A3-CC3C-4CF1-BC21-A53682BC7C7A",
"label": "other",
"number": "(707) 555-1854",
},
],
},
Object {
"contactType": "person",
"firstName": "David",
"id": "E94CD15C-7964-4A9B-8AC4-10D7CFB791FD",
"imageAvailable": false,
"lastName": "Taylor",
"name": "David Taylor",
"phoneNumbers": Array [
Object {
"countryCode": "us",
"digits": "5556106679",
"id": "FE064E55-C246-45F0-9C48-822BF65B943F",
"label": "home",
"number": "555-610-6679",
},
],
},
]
//Sample of numbers
numbers = [
(707) 555-1854,
555-610-6679
]
//Expected
filteredContacts = //Both contacts
This should work for you:
const contacts= [
{
company: "Financial Services Inc.",
// ....
phoneNumbers:[
{
//...
number: "(555) 766-4823",
},
{
//...
number: "555-610-6679",
}
]
}
//...
];
//Sample of numbers
const numbers = [
'(707) 555-1854',
'555-610-6679'
]
const filteredContacts = contacts.filter(contact => {
let number_exists=false;
contact.phoneNumbers.map(phone => phone.number).forEach( phoneNr =>{
if(numbers.indexOf(phoneNr)>-1) number_exists=true;
}
);
return number_exists;
}
);
//Expected
console.log(filteredContacts)
This question already has answers here:
How to use a variable for a key in a JavaScript object literal?
(16 answers)
Closed 6 years ago.
I am working on an ionic 1 app, I do not know if there is a way but for example, here, is there a way I can dynamically assign a parent in this object depending on the value of the Variable?
var contact = {
"Name": "John Doe",
"available": true,
Variable: [
{
"Location": "Home",
"Number": "33"
},
{
"Location": "Work",
"Number": "22"
}
]
};
Lets say Variable = "friends" Then
var contact = {
"Name": "John Doe",
"available": true,
"Friends": [
{
"Location": "Home",
"Number": "33"
},
{
"Location": "Work",
"Number": "22"
}
]
};
I know I can use ES6, the Computed Property Names [Variable] but they are not working on older devices. Is there any alternative method?
Just plain old
var contact = {
"Name": "John Doe",
"available": true,
};
contact[Variable] = [
{
"Location": "Home",
"Number": "33"
},
{
"Location": "Work",
"Number": "22"
}
]