I want to simplify nested array of object to array of string without altering original array, but value of countries array of object get changed. How I will rectify so I'll get new array of object in newCountries and old value of array of object not get modified. i/e countries
const countries = {
"country": [{
"name": "India",
"cities": [{
"name": "Mumbai",
},
{
"name": "Delhi",
}
]
},
{
"name": "Australia",
"cities": [{
"name": "Sydney",
},
{
"name": "Melbourne",
}
]
}
]
}
// I have tried following code
var newCountries = Object.assign({}, countries);
newCountries.country.map(val => {
if (val.cities) {
var city = val.cities.map(city => {
return city.name
});
val.cities = [];
val.cities = city;
}
return val;
})
console.log('new object\n', newCountries)
console.log('old object\n', countries)
<b>output</b>
<pre>
{
"country": [
{
"name": "India",
"cities": [
"Mumbai",
"Delhi"
]
},
{
"name": "Australia",
"cities": [
"Sydney",
"Melbourne"
]
}
]
}
</pre>
Well, this is a trivial task. Just loop over each country and assign a new array of city names to the existing one by mapping the object to just the value of the "name" key.
You could also try this, assuming name is the only key in the object.
country.cities = country.cities.reduce((arr, city) => arr.concat(Object.values(city)), [])
You can even simplify this further by using Array.prototype.flatMap:
country.cities = country.cities.flatMap(city => Object.values(city))
Note: flatMap is somewhat of an experimental feature in modern browsers.
Chrome – Version 69 (2018-09-04)
Edge – No
Firefox – Version 62 (2019-09-03)
Internet Explorer – No
Opera – Version 56 (2018-09-25 → Chromium 69)
Safari – Version 12 (2018-09-17)
const countries = {
"country": [{
"name": "India",
"cities": [{
"name": "Mumbai",
}, {
"name": "Delhi",
}]
}, {
"name": "Australia",
"cities": [{
"name": "Sydney",
}, {
"name": "Melbourne",
}]
}]
}
// Loop over each country
countries.country.forEach(country => {
// Replace `{ name : "CITY_NAME" }` with just `"CITY_NAME"`
country.cities = country.cities.map(city => city.name)
})
console.log(countries);
.as-console-wrapper { top: 0; max-height: 100% !important; }
Related
I'm currently trying to code an application with javascript. It pulls data from a database and the response I'm getting is something like that:
{
"values":[
{
"name": "Munich",
"location": "Germany",
"native_lang": "German",
},
{
"name": "London",
"location": "England",
"native_lang": "English",
},
{
"name": "Rome",
"location": "Italy",
"native_lang": "Italian",
}
]
}
But I need to have the JSON like that:
[
{
"name": "Munich",
"location": "Germany",
"native_lang": "German",
},
{
"name": "London",
"location": "England",
"native_lang": "English",
},
{
"name": "Rome",
"location": "Italy",
"native_lang": "Italian",
}
]
How can I delete the parent values object in my JSON?
SHORT ANSWER:
Just access the values property like a JavaScript object.
LONG ANSWER:
You didn't post the JavaScript code snippet so it's quite difficult to give you an appropriate answer.
Assuming you have the following code:
const jsonString = getDataFromTheDB()
const jsonObject = JSON.parse(jsonObject) // still has the "values" layer
const values = jsonObject.values // what you want, without the "values" layer
// BONUS: Just in case you want to convert the object back to a JSON string but without the "values" layer
const valuesJSON = JSON.stringify(values, undefined, 2)
Based on this post :
just do this (consider json the variable that contains your json):
var key = "values";
var results = json[key];
delete json[key];
json = results;
console.log(json) will output the following:
[
{
"name": "Munich",
"location": "Germany",
"native_lang": "German",
},
{
"name": "London",
"location": "England",
"native_lang": "English",
},
{
"name": "Rome",
"location": "Italy",
"native_lang": "Italian",
}
]
But you dont even have to do the last 2 steps of the code snippet above, you could also just directly use results variable and have the same output by console.log(results).
You could take the object and create a new variable with just the array.
var vals =
{
"values":[
{
"name": "Munich",
"location": "Germany",
"native_lang": "German",
},
{
"name": "London",
"location": "England",
"native_lang": "English",
},
{
"name": "Rome",
"location": "Italy",
"native_lang": "Italian",
}
]
}
var arr = vals.values;
console.log(arr);
I don't seem to understand JavaScript enough to get why this doesn't work:
let sample = [
{
"id":90,
"name":"Fort McMurray Airport",
"city":"Fort Mcmurray",
},
{
"id":273,
"name":"Murtala Muhammed International Airport",
"city":"Lagos",
},
{
"id":1227,
"name":"San Javier Airport",
"city":"Murcia"
},
{
"id":1235,
"name":"Alcantarilla Air Base",
"city":"Murcia",
},
{
"id":1275,
"name":"Muret-Lherm Airport",
"city":"La Rochelle",
},
]
let airports = sample.filter(
function(obje) {
return Object.keys(obje).some(
function(key) {
return obje[key].includes("Murc");
}
);
}
);
I keep getting a "TypeError: obje[key].includes is not a function"
BTW, this is content from an axios call
Thanks in advance.
I solved it like so:
let query = "Murc";
let new_airports = sample.filter(sample => (sample.name.includes(query) || sample.city.includes(query) ) );
I was letting the code fly over my head. However, is there a better way?
TypeError: obje[key].includes is not a function
This error caused by id types
in your sample data, id is Number but includes() is for string only.
let sample = [{
"id": 90,
"name": "Fort McMurray Airport",
"city": "Fort Mcmurray",
},
{
"id": 273,
"name": "Murtala Muhammed International Airport",
"city": "Lagos",
},
{
"id": 1227,
"name": "San Javier Airport",
"city": "Murcia"
},
{
"id": 1235,
"name": "Alcantarilla Air Base",
"city": "Murcia",
},
{
"id": 1275,
"name": "Muret-Lherm Airport",
"city": "La Rochelle",
},
]
const res = sample.filter((item, index) => {
return Object.keys(item).some((key) => {
return item[key].toString().includes('Murc')
});
});
console.log(res);
includes might not work for a string, try indexOf instead.
Do something like this : return obje[key].indexOf("Murc") > -1;
Hey i tried to transform a json file (as a js object but I can not do it). Here is an example of my problem:
Input object
{
"peoples": [
{
"name": "Alain",
"nationality": "Italian"
},
{
"name": "John",
"nationality": "French"
},
{
"name": "FOO",
"nationality": "French"
}
]
}
Output object
{
"nationality": {
"french": {
"peoples": [{ "name": "John" }, { "name": "FOO" }]
},
"italian": {
"peoples": [{ "name": "Alain" }]
}
}
}
How can i do this ? maybe Lodash but i have not find any way to do this. Can anyone help me ?
You can use a simple reduce:
const obj = {
"peoples": [{
"name": "Alain",
"nationality": "Italian"
},
{
"name": "John",
"nationality": "French"
},
{
"name": "FOO",
"nationality": "French"
}
]
}
const output = obj.peoples.reduce((a, {nationality: n, ...rest}) => {
const x = a.nationality[n]
if (x) x.push(rest)
else a.nationality[n] = [rest]
return a
}, { nationality: {} })
console.log(output)
Note, I've used a spread operator to get the rest of the properties, so if you were to add more properties to each person, then those would be included in the new object.
I am reading a simple data set from a data.txt file. I would like to take this data and transform it into a specific object as per my example below. I have managed to get it into a somewhat usable JSON object but this is not ideal. I have included an example of the desired object.
Here is my app.js file:
let output = fs.readFileSync('./data.txt', 'UTF8')
.trim()
.split('\r\n')
.map((line) => line.split(';'))
.reduce((customers, line) => {
customers.push({
name: line[0],
product: [{
item: line[1],
serial: line[2],
year: line[3]
}]
})
return customers
}, [])
console.log(JSON.stringify(output, null, 2))
This currently the above NodeJs code returns the following array object:
[
{
"name": "Nancy",
"product": [
{
"item": "Macbook Pro",
"serial": "A34D05980FCD4303",
"year": "2019"
}
]
},
{
"name": "Nancy",
"product": [
{
"item": "iPad",
"serial": "O0403X3028423C92",
"year": "2015"
}
]
},
{
"name": "Nancy",
"product": [
{
"item": "iPhone",
"serial": "X3830238S3309230",
"year": "2017"
}
]
},
{
"name": "John",
"product": [
{
"item": "Macbook Pro",
"serial": "X2020J393983H380",
"year": "2013"
}
]
},
{
"name": "John",
"product": [
{
"item": "iPhone",
"serial": "X38320093X032309",
"year": "2015"
}
]
},
{
"name": "fluffikins",
"product": [
{
"item": "iMac",
"serial": "F392D392033X3232",
"year": "2013"
}
]
},
{
"name": "fluffikins",
"product": [
{
"item": "iPad",
"serial": "FE322230D3223S21",
"year": "2011"
}
]
}
]
What I am trying to do is get the below object returned - ideally still following the same functional approach:
[
{
"name": "Nancy",
"product": [
{
"item": "Macbook Pro",
"serial": "A34D05980FCD4303",
"year": "2019"
},
{
"item": "iPad",
"serial": "O0403X3028423C92",
"year": "2015"
},
{
"item": "iPhone",
"serial": "X3830238S3309230",
"year": "2017"
}
]
},
{
"name": "John",
"product": [
{
"item": "Macbook Pro",
"serial": "X2020J393983H380",
"year": "2013"
},
{
"item": "iPhone",
"serial": "X38320093X032309",
"year": "2015"
}
]
},
{
"name": "fluffikins",
"product": [
{
"item": "iMac",
"serial": "F392D392033X3232",
"year": "2013"
},
{
"item": "iPad",
"serial": "FE322230D3223S21",
"year": "2011"
}
]
}
]
Here is my mock data set that lives in data.txt
Nancy;Macbook Pro;A34D05980FCD4303;2019
Nancy;iPad;O0403X3028423C92;2015
Nancy;iPhone;X3830238S3309230;2017
John;Macbook Pro;X2020J393983H380;2013
John;iPhone;X38320093X032309;2015
fluffikins;iMac;F392D392033X3232;2013
fluffikins;iPad;FE322230D3223S21;2011
Instead of an array you can use Map in reduce as accumulator, use name as key in Map and club value of all keys, finally just get the values Map to get desired output
const data = `Nancy;Macbook Pro;A34D05980FCD4303;2019
Nancy;iPad;O0403X3028423C92;2015
Nancy;iPhone;X3830238S3309230;2017
John;Macbook Pro;X2020J393983H380;2013
John;iPhone;X38320093X032309;2015
fluffikins;iMac;F392D392033X3232;2013
fluffikins;iPad;FE322230D3223S21;2011`
const final = data.split('\n')
.map(v => v.split(';'))
.reduce((op, [name, item, serial, year]) => {
let obj = { item, serial, year }
if (op.has(name)) {
op.get(name).products.push(obj)
} else{
op.set(name,{name, products:[obj]})
}
return op
}, new Map())
console.log([...final.values()])
Here is a "functional version" that utilizes a Map to find duplicates in O(1):
(map => (
fs.readFileSync('./data.txt', 'UTF8')
.trim()
.split('\r\n')
.map((line) => line.split(';'))
.forEach(([name, item, serial, year]) =>
map.has(name)
? map.get(name).product.push({ item, serial, year })
: map.set(name, { name, product: [{ item, serial, year }] })
),
[...map.values()]
)(new Map)
But seriously, whats so bad about imperative style?:
const customers = new Map;
const entries = fs.readFileSync('./data.txt', 'UTF8')
.trim()
.split('\r\n');
for(const entry of entries) {
const [name, item, serial, year] = entry.split(";");
const product = { item, serial, year };
if(customers.has(name)) {
customers.get(name).product.push(product);
} else customers.set(name, { name, product: [product] });
}
const result = [...customers.values()];
You can modify the .reduce function to only add a new item to the array if there isn't one with that name. If there is, just add the product to that item's product array.
const data = `Nancy;Macbook Pro;A34D05980FCD4303;2019
Nancy;iPad;O0403X3028423C92;2015
Nancy;iPhone;X3830238S3309230;2017
John;Macbook Pro;X2020J393983H380;2013
John;iPhone;X38320093X032309;2015
fluffikins;iMac;F392D392033X3232;2013
fluffikins;iPad;FE322230D3223S21;2011`;
const result = data.trim()
.split('\n')
.map((line) => line.split(';'))
.reduce((customers, line) => {
const product = {
item: line[1],
serial: line[2],
year: line[3]
};
const customer = customers.find(({
name
}) => name === line[0]);
if (customer) {
customer.product.push(product);
} else {
customers.push({
name: line[0],
product: [product]
});
}
return customers
}, []);
console.log(result);
I have json data as below:
[
{
"id": "i_1",
"name": "abc",
"address": [
{
"city": [
"city1",
"city2"
]
},
{
"city": [
"city1",
"city2"
]
}
]
},
{
"id": "i_2",
"name": "def",
"address": [
{
"city": []
},
{
"city": []
}
]
}
]
Now, I want only that data where city array is not null. So in the above example the output should be 1st element i.e. with id i_1.
How to filter this json using jmespath library?
You can do this:
var arr = [
{
"id": "i_1",
"name": "abc",
"address": [
{
"city": [
"city1",
"city2"
]
},
{
"city": [
"city1",
"city2"
]
}
]
},
{
"id": "i_2",
"name": "def",
"address": [
{
"city": []
},
{
"city": []
}
]
}
];
console.log(jmespath.search(arr,"[?not_null(address[].city[])]"));
<script src="https://cdnjs.cloudflare.com/ajax/libs/jmespath/0.15.0/jmespath.js"></script>
You could do this in pure javascript using filter and every
const items=[{"id":"i_1","name":"abc","address":[{"city":["city1","city2"]},{"city":["city1","city2"]}]},{"id":"i_2","name":"def","address":[{"city":[]},{"city":[]}]}]
const filtered = items.filter(i => i.address.every(a => a.city && a.city.length > 0))
console.log(filtered)
This returns only if every object inside address has a non-empty city array.
var arr = [
{
"id": "i_1",
"name": "abc",
"address": [
{
"city": [
"city1",
"city2"
]
},
{
"city": [
"city1",
"city2"
]
}
]
},
{
"id": "i_2",
"name": "def",
"address": [
{
"city": []
},
{
"city": []
}
]
}
];
console.log(jmespath.search({ c: arr}, "not_null(c[].address[].city[])"));
<script src="https://cdnjs.cloudflare.com/ajax/libs/jmespath/0.15.0/jmespath.js"></script>
I don't know what it's your result. can you explain better?
You don't need to use jmespath library use filter and `every from the vanilla JS. It is more efficient.
let jsonTxt = '{"data":[{"id":"i_1","name":"abc","address":[{"city":["city1","city2"]},{"city":["city1","city2"]}]},{"id":"i_2","name":"def","address":[{"city":[]},{"city":[]}]}]}'
let jsonData = JSON.parse(jsonTxt);
let items = jsonData.data;
const result = items.filter(i => i.address.every(a => a.city && a.city.length))
console.log('id: ', result[0].id);
//using jmespath
console.log(jmespath.search({data: items}, "data[*].address[*].city"));
<script src="https://cdnjs.cloudflare.com/ajax/libs/jmespath/0.15.0/jmespath.js"></script>