How to get array value from the other array in react native? - javascript

In this code I want the value in centreValues to come from some other array
const centreValues=[
{
title:'Type',
value:centreDetail.location_type
},
{
title:'Address',
value:centreDetail.address1+centreDetail.address2
},
{
title:'City',
value:centreDetail.city
},
{
title:'State/Province',
value:centreDetail.state
},
{
title:'Postal/Zipcode',
value:centreDetail.zip
},
{
title:'Phone',
value:centreDetail.phone
},
]
my centreDetails is json like this:
centreDetails={
location_type:'some Value',
address1: 'some Value',
address2:'some Value',
....
}
I want to bring these values in centreValues array.How could I do that?

its a easy JS object and array scenario. According to your provided array it seems that you expect to have centreDetail. see my below example
const centreDetail = {
location_type: "something",
city: "something",
state: "something",
zip: "something",
phone: "something",
}
now you can call the following above object in your array
const centreValues=[
{
title:'Type',
value:centreDetail.location_type
},
{
title:'City',
value:centreDetail.city
},
{
title:'State/Province',
value:centreDetail.state
},
{
title:'Postal/Zipcode',
value:centreDetail.zip
},
{
title:'Phone',
value:centreDetail.phone
},
]
EDIT: you added json in your question now. therefor, you need a loop. use for loop or while to go through the each index of array and added in your other array. you can also use map for that also
EDIT according to your comment. are you sure about that. see i typed this all in console. it seems to be working.

Here is how you can achieve it :
const centreDetails = {
location_type:'my location',
address1: 'an address',
address2: 'another address',
phone: '516546548',
city: 'Wakanda'
}
const centreValues = Object.entries(centreDetails).map(([title, value]) => ({ title, value}))
console.log(centreValues)
You will have to convert your object into an array made out of pairs of key and values which is made using Object.entries()
Then you only have and create your desired structure using map on your array
EDIT
You can apply an additional filter function if you only want certain fields :
const centreDetails = {
location_type: 'my location',
address1: 'an address',
address2: 'another address',
phone: '516546548',
city: 'Wakanda'
}
const desiredValues = ["location_type", "address2", "city"]
const centreValues = Object.entries(centreDetails)
.filter(([title, value]) => !!desiredValues.includes(title)) //Checks if the value exists
.map(([title, value]) => ({ title, value}))
console.log(centreValues)
EDIT 2 :
If you want to have a different alias here's a way to do it :
const centreDetails = {
location_type: 'my location',
address1: 'an address',
address2: 'another address',
phone: '516546548',
city: 'Wakanda'
}
const desiredValues = {
"location_type" : "location",
"address2": "this guy\'s second address",
"city": "Hey, he lives here !"
}
const centreValues = Object.entries(centreDetails)
.filter(([title, value]) => desiredValues[title])
.map(([title, value]) => ({ title : desiredValues[title], value}))
console.log(centreValues)

to get all values of centreDetail as object result
centreValues.map(x=>x.value.centreDetail)
To transfer to another array, manage the state like this:
centreValues.map(x=> this.setState({ centreDetailArray: [...this.state.centreDetailArray, x.value.centreDetail] })
Then you can get the result from state like this:
<View>
{this.state.centreDetailArray.map(x=><Text>{x}</Text>)}
</View>

Related

Best way to return a nested object the matches a property requested

I'm trying to create a new object that only contains the a product array with the seller I req. I have an order object that has a product array. I'd like to return a specific seller. I tried:
const newOrders = orders.map((element) => {
return {
...element,
product: element.product.filter(
(seller) => seller === req.currentUser!.id
),
};
});
does mongoose have a preferred method for doing what I bring to achieve? I've read through the find queries but none of the methods seem useful to this use case.
orders: [
{
userId: "638795ad742ef7a17e258693",
status: "pending",
shippingInfo: {
line1: "599 East Liberty Street",
line2: null,
city: "Toronto",
country: "CA",
postal_code: "M7K 8P3",
state: "MT"
},
product: [
{
title: "new image",
description: "a log description",
seller: "6369589f375b5196f62e3675",
__v: 1,
id: "63737e4b0adf387c5e863d33"
},
{
title: "Mekks",
description: "Ple",
seller: "6369589f375b5196f62e3675",
__v: 1,
id: "6376706808cf1adafd5af32f"
},
{
title: "Meeks Prodyuct",
description: "long description",
seller: "63868795a6196afbc3677cfe",
__v: 1,
id: "63868812a6196afbc3677d06"
}
],
version: 1,
id: "6388138170892249e01bdcba"
}
],
Im sure this can be improved, doesn't feel that its the best way possible but it gets the result. Like the previous answer you have to find first the order the seller is in then find the products than filter the seller by the id. I'm using typescript and there's a bug https://github.com/microsoft/TypeScript/issues/50769 so you have to use the bracket notation.
const orders = await Order.find({
"product.seller": req.currentUser!.id,
});
const allOrders = orders[0].product;
const sellerOrders = allOrders.filter((obj) => {
return obj["seller"] === req.currentUser!.id;
});

Typescript/NestJS - extract object from db query response

I'm using external API to make an SQL query for a user. As a result i get matching Entity but as a set of fields, lookin like this:
[
{ IsNull: false, Name: 'Key', Value: '897', Values: null },
{ IsNull: false, Name: 'FirstName', Value: 'User', Values: null },
{ IsNull: false, Name: 'LastName', Value: 'Portal', Values: null },
{
IsNull: false,
Name: 'Email',
Value: 'some#email.com',
Values: null
},
{ IsNull: true, Name: 'Salutation', Value: null, Values: null },
{ IsNull: false, Name: 'Type', Value: '2', Values: null },
{
IsNull: false,
Name: 'LastLoggedDate',
Value: '2022-12-01 15:24:03',
Values: null
}
]
How to transform this response to end with simple object { email: 'some#email', firstName: 'User' lastName: 'Portal' } ??
I ended up with solution like this (below) but i believe there's some easiest way to do that, especially with more fields
let userRawEntity = queryResult.data.Entities[0].Fields;
const userEmail = userRawEntity.filter((obj) => { return obj.Name === 'Email' });
const userFirstName = userRawEntity.filter((obj) => { return obj.Name === 'FirstName' });
const userLastName = userRawEntity.filter((obj) => { return obj.Name === 'LastName' });
return { email: userEmail[0].Value, firstName: userFirstName[0].Value, lastName: userLastName[0].Value };
Edit:
final solution that works and looks nicer. thanks for help :)
if (queryResult.data.TotalEntityCount > 0) {
let user: {[key: string]: string | null } = {}
let userRawEntity = queryResult.data.Entities[0].Fields;
userRawEntity.forEach(data => user[data.Name] = data.Value);
return { email: user.Email, currency: user.Currency } as JwtPayload;
}
As a starting point, I would transform that entire array into an object as follows:
let dataTransformed: {[key: string]: string | null} = {}
data.forEach(d => {
dataTransformed[d.Name] = d.Value
})
Which will give you a nicer looking object as follows:
{
"Key": "897",
"FirstName": "User",
"LastName": "Portal",
"Email": "some#email.com",
"Salutation": null,
"Type": "2",
"LastLoggedDate": "2022-12-01 15:24:03"
}
You now have a familiar object with which to work with. From here, you can strip out the entries you don't want. Note, you may want to do further work in that array transformation such as checking for null values, changing keys to camel case, etc...
Another approach using lodash
_.mapValues(_.keyBy(data, "Name"), o => o.Value || o.Values);

Is there a way to transform a JavaScript object into another object with cleaner formatting?

When accessing a public register API, I receive more information than I need, and sometimes the data is returned with minor variations. I would like to delete some unnecessary fields, move nested fields to the top level, and rename them. The goal is to standardise format across several different APIs, and keep the memory requirement to a minimum. Example below:
Raw object:
[
{
startDate: "2022/08/27",
expiryDate: "2025/08/27",
party: {
type: "Business",
name: "Irregular Expressions Inc."
},
location: {
type: "Office",
address: {
locality: "Boston",
postcode: "PE21 8QR"
}
}
},
{
startDate: "2023/12/22",
expiryDate: "2024/06/22",
party: {
type: "Charity",
name: "Save the Badgers"
},
site: {
type: "Office",
address: {
locality: "Badgerton",
postcode: "BA6 6ER"
}
}
},
]
I want to transform this into a smaller, cleaner array:
[
{
startDate: "2022/08/27",
expiryDate: "2025/08/27",
partyName: "Irregular Expressions Inc.",
location: "Boston"
},
{
startDate: "2023/12/22",
expiryDate: "2024/06/22",
partyName: "Save the Badgers",
location: "Badgerton"
},
]
I have tried the below, but I'm getting an error.
module.exports = {
testTransform: (inputArray) => {
const outputArray = []
inputArray.forEach(element => {
outputArray.push({
startDate: element.startDate,
expiryDate: element.expiryDate,
partyName: element.party.name,
location: element.location.address.locality
})
})
return JSON.stringify(outputArray, null, ' ')
}
}
TypeError: Cannot read properties of undefined (reading 'address')
Am I going in the right direction, or is there a simpler way of doing this? I've searched for this type of transformation but with no luck - what am I missing?
You could take either location or site with logical OR || and later the proerties with optional chaining operator ?..
const
data = [{ startDate: "2022/08/27", expiryDate: "2025/08/27", party: { type: "Business", name: "Irregular Expressions Inc." }, location: { type: "Office", address: { locality: "Boston", postcode: "PE21 8QR" } } }, { startDate: "2023/12/22", expiryDate: "2024/06/22", party: { type: "Charity", name: "Save the Badgers" }, site: { type: "Office", address: { locality: "Badgerton", postcode: "BA6 6ER" } } }],
result = data.map(o => ({
startDate: o.startDate,
expiryDate: o.expiryDate,
partyName: o.party.name,
location: (o.location || o.site)?.address?.locality
}));
console.log(result);
Since it looks like you don't know what the outer key will be for the object with the address property, if the object will always have 4 properties, when destructuring, you can use rest syntax to collect the final property into a single object, and then take that object's values to get to the address.
const input=[{startDate:"2022/08/27",expiryDate:"2025/08/27",party:{type:"Business",name:"Irregular Expressions Inc."},location:{type:"Office",address:{locality:"Boston",postcode:"PE21 8QR"}}},{startDate:"2023/12/22",expiryDate:"2024/06/22",party:{type:"Charity",name:"Save the Badgers"},site:{type:"Office",address:{locality:"Badgerton",postcode:"BA6 6ER"}}}];
const output = input.map(({
startDate,
expiryDate,
party,
...rest
}) => ({
startDate,
expiryDate,
partyName: party.name,
location: Object.values(rest)[0].address.locality,
}));
console.log(output);
You are trying to read locality property of undefined. You could use optional chaining operator to prevent the exception throwing. So, you need to use somthing like element?.location?.address?.locality instead of element.location.address.locality.
That would require writing a function that recurcively goes throught the contents of an object an returns a non-nested object. The function below is such a function.
const flattenObject = (obj) => {
let result = {};
for (const i in obj) {
if ((typeof obj[i]) === 'object') {
const temp = flattenObject(obj[i]);
for (const j in temp) {
result[j] = temp[j];
}
}
else {
result[i] = obj[i];
}
}
return result;
};
The function can then be called on each nested object in the array. The map method of arrays would be do nicely for that step.
const result = nested.map(n => flattenObject(n))
console.table(result[0]) would produce the output below

How to extend and merge a list of objects with another list using lodash

hopefully there is someone here with enough experience to guide me through this particular issue I've found.
So I am working with this peculiar API that returns an object akin to the following:
const api_response = {
environment_list: ["dev", "non-prod"],
member_list: [
{ id: "abc-456", name: "Homer", "last-name": "Simpson", role: "admin" },
],
};
The goal I'm trying to achieve is to create a new list that contains the user id, name, role, and environment based on the environment list above so it should look like this:
[
{ id: 'abc-456', name: 'Homer', role: 'admin', environment: 'dev' },
{ id: 'abc-456', name: 'Homer', role: 'admin', environment: 'non-prod' }
]
Now what I have done is the following:
I created a variable to isolate each value that will be be merged:
const userList = _.map(api_response.member_list, (data) => ({
id: data.id,
name: data.name,
role: data.role,
}));
const environmentList = _.map(api_response.environment_list, (data) => ({
environment: data,
}));
Then these two were merged as one list of objects:
const extendAndMergeList = _.map(environmentList, (data) =>
_.map(userList, (items) => ({ ...items, environment: data.environment }))
);
And the output is close enough to my final product which is:
[
[
{ id: 'abc-456', name: 'Homer', role: 'admin', environment: 'dev' }
],
[
{
id: 'abc-456',
name: 'Homer',
role: 'admin',
environment: 'non-prod'
}
]
]
I'm probably doing something silly but would love to have some guidance in how to trim this from a list of lists to a list of objects.
another look at all the steps I took:
const _ = require("lodash");
const api_response = {
environment_list: ["dev", "non-prod"],
member_list: [
{ id: "abc-456", name: "Homer", "last-name": "Simpson", role: "admin" },
],
};
const userList = _.map(api_response.member_list, (data) => ({
id: data.id,
name: data.name,
role: data.role,
}));
const environmentList = _.map(api_response.environment_list, (data) => ({
environment: data,
}));
const extendAndMergeList = _.map(environmentList, (data) =>
_.map(userList, (items) => ({ ...items, environment: data.environment }))
);
Thank you again for any helpful response!
The easy way is to use the _.flatten method of lodash to achieve this:
const _ = require('lodash');
const data = _.map(api_response.environment_list, e =>
_.map(api_response.member_list, m => ({
id: m.id,
name: m.name,
role: m.role,
environment: e
})
); // This step achieves the exact same thing that you have achieved up until now in your question.
const flattenedData = _.flatten(data); // This is the method you need to make the array "flat".
console.log(flattenedData);
You could also attempt to do this without using lodash in this, a bit more efficient, way:
const output = [];
for (const e of api_response.environment_list) {
for (const m of api_response.member_list) {
const obj = {
id: m.id,
name: m.name,
role: m.role,
environment: e
};
output.push(obj);
}
}
i believe you can use a simple map like this:
const userList = _.map(api_response.environment_list, (env) => ({
...(_.pick(api_response.member_list, ['id', 'name', 'role' ])),
environment: env
}))
for every enviroment you return an object with the keys you want and add the environment prop
you can also use _.omit instead of _.pick to remove the "last-name" prop instead of selecting all those want.

How do I filter out empty values in an Array?

I am posting to an API, and if no values are entered in the pricing form fields, I want to send an empty array.
I have tried using the filter method but when I make the post request it sends an array with an empty object i.e. [{}]
My state is:
title: "",
ticketing: "", // is equal to 0 (paid) or 1 (free)
pricing: [
{
price: "",
currency: "",
ticketLabel: ""
}
],
When ticketing is equal to 1, no values will be inputted for price, currency and ticketLabel.
This is my submit function:
handleSubmit = async (event) => {
event.preventDefault();
const info = {
title: this.state.title,
ticketing: this.state.ticketing,
price: [
{
currency: this.state.currency,
price: this.state.price,
label: this.state.ticketLabel
}
].filter(token => Object.keys(token).length != 0)
}
And I post it as
axios
.post(`https://www.diaspora100.com/api/v1/public/events/`, {
info,
})
Currently, this posts as:
price: [{}]
0: {}
I would like it to post as:
price: []
Thanks for any help!
When ticketing is equal to 1, no values will be inputted for price,
currency and ticketLabel
maybe you want something like this
const info = {
title: this.state.title,
ticketing: this.state.ticketing,
price: this.state.ticketing !== 1 ? [
{
currency: this.state.currency,
price: this.state.price,
label: this.state.ticketLabel
}
] : []
}
in the filter part of the price you can try this
<price-array>.filter(token => token.price.length > 0)

Categories