I have a JSON object as show here:
[
{
"ID": "1",
"Country": "India",
"Value1": "100",
"Value2": "200"
},
{
"ID": "2",
"Country": "China",
"Value1": "230",
"Value2": "800"
}
]
This is the result that I am looking for:
[
['India', 100],
['China', 230],
]
I tried using Jquery $.map function but was unable to get what exactly I wanted. Any pointers would be appreciated.
You can use map function :
var arr = [
{
"ID": "1",
"Country": "India",
"Value1": "100",
"Value2": "200"
},
{
"ID": "2",
"Country": "China",
"Value1": "230",
"Value2": "800"
}
];
console.log(arr.map(country=>[country.Country, country.Value1]));
This is the code that got me what I wanted:
var testJSON=[];
for(i=0;i<data.length;i++){
testJSON.push([data[i].Country,data[i].Value1])
}
Try this :
var arr = [
{
"ID": "1",
"Country": "India",
"Value1": "100",
"Value2": "200"
},
{
"ID": "2",
"Country": "China",
"Value1": "230",
"Value2": "800"
}
];
var res = arr.map(function(item) {
return [item.Country,item.Value1];
})
console.log(res);
Output :
Working fiddle : https://jsfiddle.net/h6kxvcys/
as i understand your question you want to create make object inside object.here i modify rohith's json structure .try this .i wish you will got correct answer from this.
Try this:
var arr = [{
"ID": {
"name":"Allan" ,
"sid":"1"
},
"Country": "India",
"Value1": "100",
"Value2": "200"},{
"ID": {
"name":"Brian" ,
"sid":"2"
},
"Country": "China",
"Value1": "230",
"Value2": "800"}];
Out put:
Related
I'm newbie and sequelize aggregate function makes me confused because it gave me different result than expected. I'm trying to get a total price with sequelize.fn('sum', ...), and somehow sequelize only gives me the the total of first data of the associated model instead of summing all the result. My code as follows:
Training.findAll({
where: {
owner_id: payload
},
attributes: ['id', [sequelize.fn('sum', sequelize.col('training_classes.price')), 'totalPrice']],
include: [{
model: TrainingClass,
as: 'training_classes',
attributes: ['id', 'price'],
}],
group: ['Training.id', 'training_classes.id']
})
And the result of the query :
[
{
"id": "45",
"totalPrice": "300000",
"training_classes": [
{
"id": "94",
"price": "300000"
}
]
},
{
"id": "8",
"totalPrice": "1000000",
"training_classes": [
{
"id": "14",
"price": "1000000"
},
{
"id": "15",
"price": "300000"
},
{
"id": "16",
"price": "200000"
}
]
},
{
"id": "47",
"totalPrice": "100000",
"training_classes": [
{
"id": "97",
"price": "100000"
}
]
},
{
"id": "39",
"totalPrice": "1000000",
"training_classes": [
{
"id": "81",
"price": "1000000"
},
{
"id": "82",
"price": "300000"
}
]
},
{
"id": "24",
"totalPrice": "300000",
"training_classes": [
{
"id": "46",
"price": "300000"
}
]
},
{
"id": "6",
"totalPrice": "200000",
"training_classes": [
{
"id": "11",
"price": "200000"
}
]
},
{
"id": "49",
"totalPrice": "100000",
"training_classes": [
{
"id": "100",
"price": "100000"
},
{
"id": "99",
"price": "1000000"
},
{
"id": "101",
"price": "100000"
}
]
},
{
"id": "20",
"totalPrice": "200000",
"training_classes": [
{
"id": "38",
"price": "200000"
},
{
"id": "35",
"price": "400000"
},
{
"id": "37",
"price": "500000"
},
{
"id": "36",
"price": "100000"
}
]
},
]
as you can see, the totalPrice only from the first element of training_classes, not the entire data of it. How can I resolve this? Thank you in advance
If you want to sum all records in TrainingClass that have the same Training.id then you need to indicate no attributes for TrainingClass and indicate only Training.id in group option:
Training.findAll({
where: {
owner_id: payload
},
attributes: ['id', [sequelize.fn('sum', sequelize.col('training_classes.price')), 'totalPrice']],
include: [{
model: TrainingClass,
as: 'training_classes',
attributes: [],
}],
group: ['Training.id']
})
If you group by TrainingClass.id then you'll get grouped results for each record that has unique TrainingClass.id value and literally every record of TrainingClass has unique id value.
My JSON is as follows:
{
"sales": [{
"manager": "alberic",
"surgeon": "Dr Barry Biggins",
"amount": "300",
"country": "USA",
"percent-seller": "30",
"antiquity": "June 2017",
"date": "6"
}, {
"manager": "support",
"surgeon": "Dr Barry Biggins",
"amount": "300",
"country": "UK",
"percent-seller": "20",
"antiquity": "June 2017",
"date": "2"
}, {
...
}]
}
I want to retrieve the objects from sales where manager = "support" and date = "2". How do I go about this in jQuery?
Thanks!
Simply you can use filter() method and use your condition inside filter method function will return element if your condition become true otherwise it ignore element.
data= {"sales": [{
"manager": "alberic",
"surgeon": "Dr Barry Biggins",
"amount": "300",
"country": "USA",
"percent-seller": "30",
"antiquity": "June 2017",
"date": "6"
}, {
"manager": "support",
"surgeon": "Dr Barry Biggins",
"amount": "300",
"country": "UK",
"percent-seller": "20",
"antiquity": "June 2017",
"date": "2"
},
]
};
var matchedElements = data.sales.filter(function(element) {
return (element.manager == 'support' && element.date == '2');
});
console.log(matchedElements);
//if you want to access surgeon of first element of matchedElements
console.log(matchedElements[0].surgeon);
//if you want to access surgeon of all elements in matchedElements
for(i in matchedElements)
{
console.log(matchedElements[i].surgeon);
}
You filter the sales array.
Make sure to add the polyfill from the above link if you want to support older browsers.
var matchingSales = jsonData.sales.filter(function(sale) {
return sale.manager == 'support' && sale.date == '2';
});
var data = {
"sales": [{
"manager": "alberic",
"surgeon": "Dr Barry Biggins",
"amount": "300",
"country": "USA",
"percent-seller": "30",
"antiquity": "June 2017",
"date": "6"
}, {
"manager": "support",
"surgeon": "Dr Barry Biggins",
"amount": "300",
"country": "UK",
"percent-seller": "20",
"antiquity": "June 2017",
"date": "2"
}]
};
$.each(data.sales, function(i, v) {
if (v.manager == 'support' && v.date == '2') {
console.log(v.manager)
console.log(v.surgeon)
console.log(v.amount)
}
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
Iterate over them using .each()
I'm not sure the best way to go about this. I want to iterate my json and find all companies that are in the US for example. This JSON might get way more complex as my app grows too, as in levels, objects, etc. I just want to know ways people are doing simple searching for filtering out subsets of data with JSON and Node.js and/or ES6 or libraries maybe such as Lodash, etc.
So for example this json, what are some ways I can search it and pull back only those companies in the USA?
[{
"id": 0,
"name": "Company1",
"logoUrl": "/lib/assets/company1-logo.png",
"location":{
"country": "USA",
"state": "California",
"city": "Napa"
},
"active": false
},
{
"id": 1,
"name": "Company2",
"logoUrl": "/lib/assets/company2-logo.png",
"location":{
"country": "Germany",
"state": "",
"city": "Berlin"
},
"active": false
},
{
"id": 2,
"name": "Company3",
"logoUrl": "/lib/assets/company3-logo.png",
"location":{
"country": "USA",
"state": "Michigan",
"city": "Detroit"
},
"active": false
}]
Use JavaScript native Array#filter method with ES6 arrow function
var res = data.filter(v => v.location.country === 'USA');
var data = [{
"id": 0,
"name": "Company1",
"logoUrl": "/lib/assets/company1-logo.png",
"location": {
"country": "USA",
"state": "California",
"city": "Napa"
},
"active": false
}, {
"id": 1,
"name": "Company2",
"logoUrl": "/lib/assets/company2-logo.png",
"location": {
"country": "Germany",
"state": "",
"city": "Berlin"
},
"active": false
}, {
"id": 2,
"name": "Company3",
"logoUrl": "/lib/assets/company3-logo.png",
"location": {
"country": "USA",
"state": "Michigan",
"city": "Detroit"
},
"active": false
}];
var res = data.filter(v => v.location.country === 'USA');
console.log(res);
You can use JavaScript's simple .filter() method to return the list of results fulfilling the filter. Say your data is in variable data
ES5
data.filter(function(item) {
return item.location.country === 'USA';
});
ES6: In ES6 you can use arrow functions for same as
data.filter((item) => {
return item.location.country === 'USA';
});
var data = [{
"id": 0,
"name": "Company1",
"logoUrl": "/lib/assets/company1-logo.png",
"location":{
"country": "USA",
"state": "California",
"city": "Napa"
},
"active": false
},
{
"id": 1,
"name": "Company2",
"logoUrl": "/lib/assets/company2-logo.png",
"location":{
"country": "Germany",
"state": "",
"city": "Berlin"
},
"active": false
},
{
"id": 2,
"name": "Company3",
"logoUrl": "/lib/assets/company3-logo.png",
"location":{
"country": "USA",
"state": "Michigan",
"city": "Detroit"
},
"active": false
}];
var res1 = data.filter(function(item) {
return item.location.country === 'USA';
});
const res2 = data.filter((item) => {
return item.location.country === 'USA';
});
console.log(res1);
console.log(res2);
In lodash it will be
_.filter(data, function(item) {
return item.location.country === 'USA';
});
You can use native filter function.
const items = [{
"id": 0,
"name": "Company1",
"logoUrl": "/lib/assets/company1-logo.png",
"location":{
"country": "USA",
"state": "California",
"city": "Napa"
},
"active": false
},
{
"id": 1,
"name": "Company2",
"logoUrl": "/lib/assets/company2-logo.png",
"location":{
"country": "Germany",
"state": "",
"city": "Berlin"
},
"active": false
},
{
"id": 2,
"name": "Company3",
"logoUrl": "/lib/assets/company3-logo.png",
"location":{
"country": "USA",
"state": "Michigan",
"city": "Detroit"
},
"active": false
}]
const usItems = items.filter(v => v.location.country === 'USA')
I want to make a http request to a url lets say
http://test1.com/info this will give me xml
I want to convert
this xml from 1 to json lets say json 1
Now I make a rest request to url lets
say http://test2.com/myweb this returns a json lets say json 2
json 1
[
{
"id": "123",
"testname": "test123",
"name": "John Doe",
"active": true,
"type": "test6"
}
{
"id": "456",
"testname": "test564",
"name": "Ship Therasus",
"active": true,
"type": "test7"
}
.... some 100 entries
]
and json 2 some like below
[
{
"id": "123",
"country": "USA",
"state": "KA",
"age": 24,
"group": "g1"
}
{
"id": "456",
"country": "UK",
"state": "MA",
"age": 28,
"group": "G2"
}
...... 100 entries
]
Now Id is the constant thing between json1 and json2 I want to make a resultant json something like below lets call json3.I want to match the id and get country and state from json2 and append to json1
[
{
"id": "123",
"testname": "test123",
"name": "John Doe",
"active": true,
"type": "test6",
"country":"USA",
"state":"KA"
}
{
"id": "456",
"testname": "test564",
"name": "Ship Therasus",
"active": true,
"type": "test7",
"country":"UK",
"state":"MA"
}
]
Now wat i tried for 1 and 3 but for 2 dont know wat to do and to compare and combine I am not sure wat to do If any help will be greatful
function fun()
{
var data="hello";
$.post('http://localhost/ws/service.asmx/HelloWord',{},function(response)
{ data = response;
}).error(function(){
alert("Sorry could not proceed");
});
return data;
}
you can try this:
<script>
$(document).ready(function(){
var result=[];
var x=[{"id": "123","testname": "test123", "name": "John Doe","active": true,"type": "test6"},{"id": "456", "testname": "test564", "name": "Ship Therasus", "active": true,"type": "test7" }];
var y = [{ "id": "123", "country": "USA", "state": "KA", "age": 24,"group": "g1"},{ "id": "456", "country": "UK", "state": "MA", "age": 28, "group": "G2"}];
for (i in x){
for (j in y){
if(x[i].id == y[j].id){
result.push($.extend(x[i], y[j]));
}
}
}
console.log(result);
});
</script>
I would like to display the data within a json file in javascript. my Json file is the following:
{
"structure": [
{
"country": "Ecuador",
"sport": "soccer",
"id": "545",
"age": "23",
"gender": "male"
},
{
"country": "USA",
"sport": "Golf",
"age": "22",
"id": "2",
"gender": "female"
},
{
"gender": "male",
"id": "3",
"sport": "Basketball",
"age": "21",
"country": "Argentina"
},
{
"gender": "female",
"sport": "Tennis",
"id": "4",
"age": "20",
"country": "Colombia"
},
{
"country": "Venezuela",
"gender": "male",
"age": "19",
"sport": "Volleyball",
"id": "5"
}
]
}
and my javascript request the json file is the following:
var setup = function() {
var xhr = new XMLHttpRequest();
async = true;
xhr.open("GET", "output.json", async);
xhr.onload = function(){
var finalResult = JSON.parse(this.responseText);
console.log(finalResult['structure']);
};
/*kick off the request*/
xhr.send();
};
can anyone help me to display the actual data please?
JSON is already text. Log it before you parse it: console.log(this.responseText)