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>
Related
i have a fetch that return a json object like this:
{
"id": "xxxxxxxxxxxxxxxxxxx2",
"name": "name",
"instant_invite": "link",
"channels": [
{
"id": "xxxxxxxxxxxxxxxxxxx2",
"name": "Altri Giochi",
"position": 8
},
{
"id": "xxxxxxxxxxxxxxxxxxx2",
"name": "Ascolto Musica",
"position": 11
},
],
"members": [
{
"id": "0",
"username": "xxxxxxxxxxxxxx",
"discriminator": "0000",
"avatar": null,
"status": "idle",
"avatar_url": "https://url"
},
],
"presence_count": 3
}
now i'm trying to map and display all these items to make an unordered list for my website, but i can't map this object with this.myResponse.map...i get an error "map is not a function".
i'm definitely lost here and i need some help to make an unordered list that contain all members name, avatar and other stuff.
can someone help me on this in vanilla js?
You can loop through the object using Object.keys(). Here's an example
let data = {
"id": "xxxxxxxxxxxxxxxxxxx2",
"name": "name",
"instant_invite": "link",
"channels": [{
"id": "xxxxxxxxxxxxxxxxxxx2",
"name": "Altri Giochi",
"position": 8
},
{
"id": "xxxxxxxxxxxxxxxxxxx2",
"name": "Ascolto Musica",
"position": 11
},
],
"members": [{
"id": "0",
"username": "xxxxxxxxxxxxxx",
"discriminator": "0000",
"avatar": null,
"status": "idle",
"avatar_url": "https://url"
},
{
"id": "1",
"username": "yyyyyyyyyyyyy",
"discriminator": "0000",
"avatar": null,
"status": "idle",
"avatar_url": "https://url"
},],
"presence_count": 3
}
data.members.forEach(m => {
// start the UL
let ul = "<ul class='member'>";
// loop through using Object.keys(m) or preset array of keys
['username','avatar'].forEach(md => {
ul += `<li>${md}: ${m[md]}</li>`;
})
ul += "</ul>";
document.querySelector('#members').innerHTML += ul;
})
<div id='members'></div>
I have installed a Json-Server. Get-Requests to this server are delivering the expected data but now I want to add some search queries to my request. But the result is still the same. I don't know whats my mistake here.
Here is my request:
http://localhost:3000/people?age=22
I also tried it with:
http://localhost:3000/people?customer.age=22 but the result is still all data.
Thats my JSON-File:
{
"customer": [
{
"id": 1,
"name": "Stefan Winkler",
"phone_number": "017692601589",
"age": "22",
"education": "High School"
},
{
"id": 2,
"name": "Christoph Huber",
"phone_number": "094462649",
"age": "42",
"education": "nothing"
},
{
"id": 3,
"name": "Michael Unholer",
"phone_number": "093862649",
"age": "12",
"education": "Realschule"
}
]
}
try
http://localhost:3000/customer?age=22
[
{
"id": 1,
"name": "Stefan Winkler",
"phone_number": "017692601589",
"age": "22",
"education": "High School"
}
]
http://localhost:3000/customer?name_like=rist
[
{
"id": 2,
"name": "Christoph Huber",
"phone_number": "094462649",
"age": "42",
"education": "nothing"
}
]
I've been searching for something to help me on this particular problem but I've not found a suitable solution which works for my case. I'd really appreciate it if somebody could point me in the right direction.
I have an array:
var ruizTreeData = [
{
"Name": "Ruiz Hernandez",
"parent": "null",
"nodetype": "person",
"Country": "assets/img/aml-flag-1.png",
"children": [
{
"Name": "Checking",
"nodetype": "account",
"Country": "assets/img/aml-flag-1.png",
"children": [
{
"Name": "Renato Godoy",
"nodetype": "person",
"Country": "assets/img/aml-flag-4.png"
},
{
"Name": "Juan Nieto",
"nodetype": "person",
"Country": "assets/img/aml-flag-4.png"
},
{
"Name": "Bani Cortes",
"nodetype": "person",
"Country": "assets/img/aml-flag-2.png"
},
{
"Name": "Medina Marquez",
"nodetype": "person",
"Country": "assets/img/aml-flag-2.png"
}
]
}
]
}
];
I have another function which is creating an object, and what I'd like to do is insert that object into this "tree" at a particular index. So for example, the user would choose to insert a new person underneath "Ruiz Hernandez", so I'd create an object which would be a sibling of "Checking". But I want it to be flexible enough so that if somebody added as a child of "Checking" we'd do the same, or a child of "Bani Cortes" we'd do the same etc.
I'm thinking I'd have to iterate ruizTreeData until I found "Ruiz Hernandez" and then insert the object into the children array of "Ruiz Hernandez".
So the array would then look like:
var ruizTreeData = [
{
"Name": "Ruiz Hernandez",
"parent": "null",
"nodetype": "person",
"Country": "assets/img/aml-flag-1.png",
"children": [
{
"Name": "Checking",
"nodetype": "account",
"Country": "assets/img/aml-flag-1.png",
"children": [
{
"Name": "Renato Godoy",
"nodetype": "person",
"Country": "assets/img/aml-flag-4.png"
}...
]
},
{
"Name": "Inserted object",
"nodetype": "account",
"Country": "assets/img/aml-flag-1.png"
}
]
}
];
I've tried a bunch of things, the closest I've got is:
positionInTree = ruizTreeData
.map(function (element) {return element["Name"];})
.indexOf("Ruiz Hernandez");
But this only returns at the first index, it doesn't go into the nested objects. Would I need a for loop for this?
I'd really appreciate some help with this. Thanks very much.
There you go. You can select your element by a key/value identifier and this function will create a children property if there isn't one already with your object inside it or add your object into the already existing children property.
I added a person named Ruiz Hernandez into the last node (Checking's children) to show you how the function add your object in every node with your key/value identifier.
var ruizTreeData = [
{
"Name": "Ruiz Hernandez",
"parent": "null",
"nodetype": "person",
"Country": "assets/img/aml-flag-1.png",
"children": [
{
"Name": "Checking",
"nodetype": "account",
"Country": "assets/img/aml-flag-1.png",
"children": [
{
"Name": "Renato Godoy",
"nodetype": "person",
"Country": "assets/img/aml-flag-4.png"
},
{
"Name": "Juan Nieto",
"nodetype": "person",
"Country": "assets/img/aml-flag-4.png"
},
{
"Name": "Bani Cortes",
"nodetype": "person",
"Country": "assets/img/aml-flag-2.png"
},
{
"Name": "Ruiz Hernandez",
"nodetype": "person",
"Country": "assets/img/aml-flag-2.png"
},
{
"Name": "Medina Marquez",
"nodetype": "person",
"Country": "assets/img/aml-flag-2.png"
}
]
}
]
}
];
Array.prototype.addChild = function(key, value, object){
let el;
if(this.find(x => x[key] == value)){
this.filter(x => x[key] == value).forEach(y => y.children = (y.children) ? [...y.children, {...object}] : [{...object}])
}
this.forEach(y => {
if(y.children){
y.children.addChild(key, value, object);
}
});
}
ruizTreeData.addChild("Name", "Ruiz Hernandez", {Name: "Name"});
console.log(ruizTreeData);
This is format I got from Firebase
{
"-KpigwmSqAYFFaWgHYZ5": {
"name": "chomroeun",
"password": "123",
"phone": "093909157",
"team": "Ekerk"
},
"-KpihLkWatPmYJBlVjCR": {
"name": "hello",
"password": "123",
"phone": "322344",
"team": "eer"
},
"-KpihrxRw_L5Z2f5Jceb": {
"name": "visal",
"offline_image": {
"-KpjPf3fO8Vz1rVu-lvl": "content://com.animalmama.innov8_dealer/my_images/id_card_PP00003_20170723_192720_1055007560.jpg",
"-KpjPjtIGjHoL_bvKF5x": "content://com.animalmama.innov8_dealer/my_images/id_card_PP00003_20170723_192720_1055007560.jpg",
"-KpjPjtJcTAIL3PZTjX9": "content://com.animalmama.innov8_dealer/my_images/id_card_PP00003_20170723_192739_473034014.jpg"
},
"password": "1234",
"phone": "069331000",
"team": "k01"
},
"-KpiiPhpXxDHZd4JRYNw": {
"name": "Hongda",
"password": "123",
"phone": "098776688",
"team": "Hog"
},
"-KpiiX8Xf9fWHAheIJlm": {
"name": "Hongda",
"password": "123",
"phone": "098776688",
"team": "Hog"
},
"-KpiiixW0jRegBk6-CL2": {
"name": "TT",
"password": "123",
"phone": "059544",
"team": "dd"
}
}
But what I want the format like this
{ "aaData": [
[ "1", "Armand", "Warren", "56045", "Taiwan, Province of China" ],
[ "2", "Xenos", "Salas", "71090", "Liberia" ],
[ "3", "Virginia", "Whitaker", "62723", "Nicaragua" ],
[ "4", "Kato", "Patrick", "97662", "Palau" ],
[ "5", "Penelope", "Hensley", "76634", "Greenland" ],
[ "6", "Georgia", "Erickson", "81358", "Bolivia" ]
]
}
When youre working wirh firebase , arrays not your friend.
However if you want to turn objects into arrays you can use map function for iterate trough root object props and return an array
Object.keys(asd).map(function(a,i){
return[i,asd[a].name,asd[a].password]
})
Here is a plunker
I've never tried map/reduce.
How would I get the oldest of each type of animal?
My data is like this:
[
{
"cateory": "animal",
"type": "cat",
"age": 4,
"id": "a"
},
{
"cateory": "animal",
"type": "bird",
"age": 3,
"id": "b"
},
{
"cateory": "animal",
"type": "cat",
"age": 7
"id": "c"
},
{
"cateory": "animal",
"type": "bird",
"age": 4,
"id": "d"
},
{
"cateory": "animal",
"type": "cat",
"age": 8,
"id": "e"
},
{
"cateory": "company",
"type": "Internet",
"age": 5,
"id": "Facebook"
}
]
I'm using node-mongodb-native. Thanks!
Your map function should look something like this:
map = function() {
emit({type: this.type}, {age: this.age});
}
And the reduce function:
reduce = function(key, values) {
maxAge = 0;
values.forEach(function(v) {
if (maxAge < v['age']) {
maxAge = v['age'];
}
});
return {age: maxAge};
}
It's pretty simple:
collection.find({type : 'animal'}).sort({animal: -1}).limit(1);