convert nested object to flat data - javascript

Hi every one i have a data like this :
{
id: 1,
address: {id:100 , street: 'Kulas Light', suite: 'Apt. 556', city: 'Gwenborough', {…}},
company: {id:200,name: 'Romaguera-Crona', catchPhrase: 'Multi-layered client-server neural-net'},
email: "Sincere#april.biz",
}
i need too set value of id to address and company
how can i convert it to this :
{
id: 1,
address:100
company:200
email: "Sincere#april.biz",
}
Object.entries(record)
.filter((item) => typeof item[1] === "object")
.forEach((items: any) => `${items[0]}:${items[1]?.id}`);

If you want to use .filter, you want the output object to have the same number of keys as the input object, so overwrite the key you're iterating over in the forEach.
const record = {
id: 1,
address: {id:100 , street: 'Kulas Light', suite: 'Apt. 556', city: 'Gwenborough'},
company: {id:200,name: 'Romaguera-Crona', catchPhrase: 'Multi-layered client-server neural-net'},
email: "Sincere#april.biz",
};
Object.entries(record)
.filter(entry => typeof entry[1] === "object")
.forEach(([key, value]) => {
record[key] = value.id;
});
console.log(record);
A nicer way without mutating the existing object would be to use Object.fromEntries to transform the value part of each entry if necessary.
const record = {
id: 1,
address: {id:100 , street: 'Kulas Light', suite: 'Apt. 556', city: 'Gwenborough'},
company: {id:200,name: 'Romaguera-Crona', catchPhrase: 'Multi-layered client-server neural-net'},
email: "Sincere#april.biz",
};
const result = Object.fromEntries(
Object.entries(record)
.map(([key, value]) => [
key,
typeof value === 'object' ? value.id : value
])
);
console.log(result);
typeof null gives 'object' too, though - if that's a possibility, you can instead use for the value:
value?.id ?? value

Related

How do I turn an array of JavaScript objects into an URL string with only one loop?

I am trying to turn an array of JavaScript objects into a URL string with params, as seen below:
const objects = [{
firstName: "John",
lastName: "Doe",
age: 46
},
{
country: "France",
lastName: "Paris"
}
]
let entry_arr = [];
objects.forEach(obj => {
Object.entries(obj).forEach(entry => {
entry_arr.push(entry.join('='));
});
});
let entry_str = entry_arr.join('&');
console.log(entry_str);
By all appearances, the above code works. There is a problem though.
The problem
As you can see, I have 2 nested forEach loops. For better performance, I wish I knew how to avoid this nesting and instead use only one forEach loop.
How can I achieve the same result with inly one loop?
Merging each object inside the array in a main one would be the only solution I can think of. ( I assumed lastName: paris was a typo)
const objects = [{
firstName: "John",
lastName: "Doe",
age: 46
},
{
country: "France",
city: "Paris"
}
]
const obj = objects.reduce((acc, obj) => Object.assign(acc, obj), {});
const entry_str = Object.entries(obj).map(entry =>
entry.join('=')
).join('&');
console.log(entry_str);
The answer provided by Nina stil uses a nested loop, instead of using forEach, map is used.
For special character handling i strongly recommend to use the URLSearchParams class.
I would advise to join the array of objects in one single objects with reduce and import the entries in a URLSearchParams
const objects = [
{ firstName: "John", lastName: "Doe", age: 46 }, { country: "France", lastName: "Paris" }];
let reduced = objects.reduce((acc, object) => Object.assign(acc, object))
let params = new URLSearchParams(reduced);
console.log(reduced);
console.log(params.toString());
At least, you need to map the entries with joining key and value and join the nested entries and outer array.
const
objects = [{ firstName: "John", lastName: "Doe", age: 46 }, { country: "France", lastName: "Paris" }],
result = objects
.map(object => Object
.entries(object)
.map(entry => entry.join('='))
.join('&')
)
.join('&');
console.log(result);

How to merge nested json in snowflake?

object.assign is only perform the direct merge but its not working for nested json.
If anyone worked on this, could you please share the steps.
For example,I want to update the phone number and city of the user. City is under the location property. How should i update the value of city?
Example:
const user = {
name: "Liya",
phone: 12345,
location: {
city: "Camden",
country: "UK"
}
};
const updates = {
name: "David",
phone: 12345678,
location: {
city: "Smithfield"
}
};
Output should be like this:
console.log(Object.assign({}, user, updates));
{
name: 'Liya',
phone: 12345678,
location: {
country: 'UK',
city: 'Smithfield'
}
}
I'm assuming the name should be David since that's the name in the updates.
Based on #Han Moe Htet's comment, I used code from Vincent on that response. I used his because it does not require any external libraries, which Snowflake currently does not allow for Javascript UDFs.
There's an important consideration with this code. It uses recursion, and Snowflake UDFs have rather limited stack depth. If you have a highly nested object, it could run out of stack depth.
set USR = $${
name: "Liya",
phone: 12345,
location: {
city: "Camden",
country: "UK"
}
}$$;
set UPDATES = $${
name: "David",
phone: 12345678,
location: {
city: "Smithfield"
}
}$$;
create or replace function MERGE_OBJECTS("obj1" object, "obj2" object)
returns object
language javascript
strict immutable
as
$$
return merge(obj1, obj2);
function merge(current, updates) {
for (key of Object.keys(updates)) {
if (!current.hasOwnProperty(key) || typeof updates[key] !== 'object') current[key] = updates[key];
else merge(current[key], updates[key]);
}
return current;
}
$$;
with x as
(
select parse_json($USR) as USR, parse_json($UPDATES) as UPDATES
)
select merge_objects(USR, UPDATES) from X;

Function Traversing Through An Array of Json Object needs to return one object

So I am not the best at writing functions so I am having a hard time wrapping a head around this. So I am trying to create a function that traverses through array of objects, and stops once it reads the name that is given it must return the number of the that person.
const array = [{name: 'Ann', phone: '575.580.1400', role: 'Developer'},
{name: 'Ben', phone: '575.641.4041', role: 'Manager'},
{name: 'Clara', phone: '512.717.5690', role: 'Developer'}];
const getNumber = (person, book ) => {
for (var x of book ) {
if( x == person) {
return number;}
return ('Not found');
}
}
I know I am missing how to call in the number, but I just can't think of how to do it.
First you need to access the key inside the object and return ('Not found'); is not in the right place. Secondly use === instead of ==.In your code if the function will return in the fist iteration only. Because if you search for Clara and in the if condition Ann will not be equal to Clara so it will return Not Found and will not iterate the remaining array
const array = [{
name: 'Ann',
phone: '575.580.1400',
role: 'Developer'
},
{
name: 'Ben',
phone: '575.641.4041',
role: 'Manager'
},
{
name: 'Clara',
phone: '512.717.5690',
role: 'Developer'
}
];
const getNumber = (person, book) => {
for (var x of book) {
if (x.name === person) {
return x.phone;
}
}
return ('Not found');
}
console.log(getNumber('Clara', array))
Alternatively you can also use array methods like find or filter
const array = [{
name: 'Ann',
phone: '575.580.1400',
role: 'Developer'
},
{
name: 'Ben',
phone: '575.641.4041',
role: 'Manager'
},
{
name: 'Clara',
phone: '512.717.5690',
role: 'Developer'
}
];
const num = array.find(item => item.name === 'Clara').phone;
console.log(num)
Below code helps to iterate through a JSON object and print the required value. You can use the required conditions in IF and print the desired values accordingly.
var json = [{name: 'Ann', phone: '575.580.1400', role: 'Developer'},
{name: 'Ben', phone: '575.641.4041', role: 'Manager'},
{name: 'Clara', phone: '512.717.5690', role: 'Developer'}];
for (var key in json) {
if (json.hasOwnProperty(key)) {
if(json[key].name=='Ben')
console.log(json[key].phone);
}
}
Try this
I am using some instead of foreach or other loops. some() method executes the callback function once for each element present in the array until it finds the one where callback returns a truthy value.
const getNumber = (person, book ) => {
let pnumber;
book.some((item) => {
if(item.name.toLowerCase()===person.toLowerCase()){
pnumber = item.phone;
return true;
}
})
return pnumber ? pnumber: "Not Found" ;
}
you can call it this way
getNumber('clara',array)
"512.717.5690"
getNumber('ben1',array)
"Not Found"

javascript - map with conditionally altered nested field

Given an array such as:
people = [
{
name: 'Bob',
sex: 'male',
address:{
street: 'Elm Street',
zip: '12893'
}
},
{
name: 'Susan',
sex: 'female',
address:{
street: 'Hickory Street',
zip: '00000'
}
}
]
I am trying to write a function which will alter specific instances of '00000' in the nested field 'zip' to the string '12893' and return a new array identical to the initial array except with the corrected values. My attempt at a function so far is:
function zipFix (initialArray) {
return initialArray.map(function(person) {
if(person.address.zip === '00000')
person.address.zip = "12893"
return person
});
}
I know this function is altering the values in 'initialArray', which isn't supposed to happen. How can I go about writing my function so that I can effectively use the map function to create a new, corrected array? Thanks.
While map-ing over the values, you will need to create a copy of each object. The easiest way to do so is with the object spread syntax ({...obj}).
This will "spread" all the values (name, adress, etc) into a new object. So any changes won't mutate it. However, it's "shallow" meaning it will be a new object but its values are the same. So since address is also an object we need to copy that as well, hence the reason for the nested spread of the address value as well.
people = [{
name: 'Bob',
sex: 'male',
address: {
street: 'Elm Street',
zip: '12893'
}
},
{
name: 'Susan',
sex: 'female',
address: {
street: 'Hickory Street',
zip: '00000'
}
}
]
function zipFix(initialArray) {
return initialArray.map(function(person) {
// Create a new "copy" of the person. Using object spread
// will create a "shallow" copy, so since address is also an
// object it will have to be spread (same for other objects that might
// be mutated).
const newValue = { ...person, address: { ...person.address }}
if (newValue.address.zip === '00000') {
newValue.address.zip = "12893";
}
return newValue
});
}
console.log(zipFix(people))
console.log(people) // unchanged
You need to return values from callback function too, also make a copy of element before assigning to avoid mutability
const people = [{name: 'Bob',sex: 'male',address:{street: 'Elm Street',zip: '12893'}},{name: 'Susan',sex: 'female',address:{street: 'Hickory Street',zip: '00000'}}]
function zipFix (initialArray) {
return initialArray.map(function(person) {
let newObj = JSON.parse(JSON.stringify(person))
if(newObj.address.zip === '00000')
newObj.address.zip ="12893"
return newObj
});
}
console.log(zipFix(people))
people = [{
name: 'Bob',
sex: 'male',
address: {
street: 'Elm Street',
zip: '12893'
}
},
{
name: 'Susan',
sex: 'female',
address: {
street: 'Hickory Street',
zip: '00000'
}
}
]
function zipFix (initialArray) {
return (initialArray.map(({address, ...p}) => (
address.zip !== '00000' ? { ...p, address } : {
...p,
address: {
...address,
zip: '12893'
}
}
)));
}
console.log(zipFix(people));
You can do:
const people = [{name: 'Bob',sex: 'male',address: {street: 'Elm Street',zip: '12893'}},{name: 'Susan',sex: 'female',address: {street: 'Hickory Street',zip: '00000'}}]
const zipFix = people.map(({address, ...p}) => ({
...p,
address: {
...address,
zip: address.zip === '00000' ? '12893' : address.zip
}
}))
console.log(zipFix)

javaScript startsWith method giving an error

I am getting an error when filtering an array with startsWith method.
Error: Cannot read property startsWith of undefined
Here is my array:
let testdata = [
{
_id: "5d0876833827c2176cae90df",
MobileNumber: "965XXXXXXX",
Keyword: "ACCESSORIES",
DateStamp: 1560835715501,
id: "5d0876833827c2176cae90df"
},
{
_id: "5d0876833827c2176cae90e0",
MobileNumber: "965XXXXXXX",
Keyword:
"ACCESSORIES, ANNIVERSARY, BABY, BAGS, BATHING SUIT, BELTS,
BIRTHDAY, BIRTHDAY GIFT, BRAND, BRANDS, CHILDREN, CLOTHING,
DateStamp: 1560835715501,
id: "5d0876833827c2176cae90e0"
},
{
_id: "5d0876833827c2176cae90e1",
MobileNumber: "965XXXXXXX",
Keyword:
"ACCESSORIES, ANNIVERSARY, BABY, BAGS, BATHING SUIT, BELTS,
BIRTHDAY, BIRTHDAY GIFT, BRAND, BRANDS, CHILDREN, CLOTHING,
COMFORT, DEALS, DISCOUNT, DRESS, DRESSES, EXCHANGE, FASHION,
GIFT, GIFT CARD, GLASSES, HAIR.",
DateStamp: 1560835715501,
id: "5d0876833827c2176cae90e1"
},
{
_id: "5d08c7c79d70334824470fb4",
Name: "JOHN",
MobileNumber: "961XXXXXXX",
AnotherNumber: "NULL",
Email: "NULL",
FamilyName: "SMITH",
Gender: "M",
DateStamp: 1560856519847,
id: "5d08c7c79d70334824470fb4"
},
{
_id: "5d08c7c79d70334824470fb6",
Name: "ANTHONY",
MobileNumber: "961XXXXXXX",
AnotherNumber: "NULL",
Email: "NULL",
FamilyName: "JR",
Gender: "M",
DateStamp: 1560856519848,
id: "5d08c7c79d70334824470fb6"
},
{
_id: "5d0884ef3827c2176cb2a970",
MobileNumber: "96170359896",
PlateNumber: "NULL",
CarModel: "NULL",
CarType: "NULL",
DateStamp: 1560839407029,
id: "5d0884ef3827c2176cb2a970"
},
{
_id: "5d0884ef3827c2176cb2a971",
MobileNumber: "961XXXXXXXX",
PlateNumber: "P293676",
CarModel: "SEDAN",
ProductionDateOfCar: 1483228800000,
PurchaseDateOfCar: 1499281200000,
CarType: "HONDA",
DateStamp: 1560839407029,
id: "5d0884ef3827c2176cb2a971"
}
];
console.log(testdata.filter(d => d.Keyword.startsWith('ACCESS))); //getting error
i was expecting to have all the records those start with 'ACCESS'.
How to apply startsWith method on multiple objects having different properties within same array?
You need to check if the Keyword property exists first :
console.log(testdata.filter(d => d.Keyword && d.Keyword.startsWith('ACCESS')));
You have plenty of objects that don't have the KeyWord property, so you have to account for those cases too:
testdata.filter(d => d.KeyWord && d.Keyword.startsWith('ACCESS'));
Or, if the KeyWord property can potentially be of a type other than string:
testdata.filter(d => typeof d.KeyWord === 'string' && d.Keyword.startsWith('ACCESS'));
there are some objects which does not have keyword. First check of its existence.
console.log(testdata1.filter(d =>d.Keyword ? d.Keyword.startsWith('ACCESS') : false))

Categories

Resources