Jquery: Iterating over nested JSON with unique key names - javascript

I have a large list of objects inside JSON like this:
var data = {
4eae1aa12efa83745d00000b: {
location: "office",
latLong: [
40.7069546, -74.0094471
],
},
4eae1aa12efa83745d000000: {
location: "home",
latLong: [
42.3584308, -71.0597732
]
}
};
Where the 4eae1aa12efa83745d00000b style key is random. How do I iterate through the JSON to print the location and latLong array of each nested JSON object?
I tried:
$.each(data, function() {
$.each(this, function() {
console.log(this.location);
});
});
but that doesn't return anything

You should look up the $.map function to translate the items in your object/array - Go for something like this:
$.map(data, function(val, i){
console.log(val.location);
console.log(val.latLong[1]);
console.log(val.latLong[2]);
})
I believe that's what you're after anyway.

Your only problem is that you don't need the inner loop.
$.each(data, function(id, value) {
console.log(value.location);
});

You are trying to loop over the properties of an object. To do this:
for(var prop in obj) {
if(obj.hasOwnProperty(prop))
// do something with obj[prop].latLng
}
.

Related

psuhing an object inside another object

I want to create an object of objects which should be something like this.
let data={
{ _id:10010,
value:"tom"
},
{
_id:2002,
value:22882
}
}
One solution that i could think of was this .
let data = {};
data.content = ({
_id: 1001,
value: "tom"
});
data.content = ({
id: 10001,
status: "harry"
});
console.log(data);
However if we do this we can only have one content inside our main object .Can we accomplish the desired data format while creating an object of objects ?
You can use array.
let data=[
{ _id:10010,
value:"tom"
},
{ _id:2002,
value:22882
}
]
data.push({
_id:1001,
value:"tom"
});
data.push({
_id:1001,
value:"tom"
});
Push method will add object to the array. If you need something else you can create more complex function/class that handles the requirements, but this maybe would be enough.
I assume you need to do with array
let data=[];
data.push({
_id:1001,
value:"tom"
});
data.push({
id:10001,
status:"harry"
});
console.log(data);
You cannot create an "object of objects". Objects store data in key:value pairs. You might consider creating an array of objects, after which you can reference the array items using indexes:
let data = [];
data.push({
_id: 10010,
value: "tom"
});
data.push({
_id: 2002,
value: 22882
});
console.log(data);
You can make use of arrays in order to achieve the result.
let data = [];
data[0]= {
_id:10010,
value:"tom"
}
data[1]= {
_id:2002,
value:22882
}
and so on...
I believe what you really mean is
let data={
10010: { _id:10010,
value:"tom"
},
2002: {
_id:2002,
value:22882
}
}
Property of objects has to be key value pair, meaning in order to have a object nested in an object, as a value, u need to pair it with a key. Hence using the id of object as the key to store it.
data['10010'] = { _id: 10010, value: 'tom' };
data['2002'] = { _id: 2002, value: 22882 };

How to apply Filter for the below JSON Array in Angular JS

I have a JSON array in the following structure
translations ={
"English" :[
{"Localizedtext":"Submit",
"TextCode":1},
{"LocalizedText":"blah"
"TextCode":2 },
{-------900 odd objects }]
"SelectedLanguage": [
"1":{"Localizedtext":"Submit",
"TextCode":1,
"TextID" :100},
"2":{"Localizedtext":"development",
"TextCode":1,
"TextID" :101},
"1008" :{"Localizedtext":"blah",
"TextCode":1,
"TextID" :102},
"80,000":{"Localizedtext":"foo",
"TextCode":1,
"TextID" :103},
{-------900 odd objects }]
}
My HTML page looks like this :
<tr ng-repeat="Translation in translations.english|searchFilter : search">
<td>{{Translation.TextCode}}</td>
<td>{{Translation.LocalizedText }}
<div><textarea ng-model="translations.selectedLangauge[Translation.TextCode].LocalizedText" ng-change="getTranslations(Translation.TextCode)" class="text-area" rows="2">{{ translations.selectedLangauge[Translation.TextCode].LocalizedText }}
</textarea>
</div>
</td>
My custom filter which i have designed looks like this :
.filter('searchFilter', function () {
return function (items, search) {
if (!search){
return items;
}
var result = [];
angular.forEach(items, function (value, key) {
//console.log(items);
angular.forEach(value, function (value2,key2) {
//console.log(value2);
if (search===value2) {
result.push(value2);
}
});
});
return result;
}
});
The filter that i have designed is applicalble only to translations.english array of the translation object .How to apply this to both the arrays of the object?
You have to concat the arrays before iterating over them. Filter will be applied on the joined arrays.
ng-repeat="Translation in
translations.english.concat( translations.SelectedLanguage )|searchFilter : search"

How to filter array of arrays?

I have a JSON data. I am pushing this data to another array. The problem i am facing is, i want to filter array whose data attrs has src property. And push these array to another array. Can anyone will help me in this.I am not getting a way to do this.
My Json data is like:
DATA:
[
{
"data":{
},
"type":"image",
"attrs":{
"x":92,
"y":163,
"width":100,
"height":100,
"src":"http://localhost:63342/wodrobs/app/scripts/views/img/top.jpg",
"cursor":"move",
"opacity":1
},
"transform":"",
"id":0
},
{
"data":{
},
"type":"path",
"attrs":{
"fill":"none",
"stroke":"#000",
"stroke-dasharray":"- ",
"opacity":0.5
},
"transform":"",
"id":17
},
]
As far as I understood from your pseudo-json, you can do like this:
//your data
var a = [
{'src':"a.src"},
{'id':"someid"},
{'src':"b.src"}
];
//the result array
var result = [];
for(i=0; i<a.length;i++){
var e = a[i];
if(e.src){
result.push(e);
}
}
console.log(result);
http://jsbin.com/hujicopuca/1/edit?html,js,console,output
I think this is what you are looking for:
function HasSrcProperty(value, index, ar) {
return value.some(elem => elem.indexOf("src") > -1)
}
var result = yourJsonArray.filter(HasSrcProperty);
For more info on javascript array filter and some:
https://msdn.microsoft.com/en-us/library/ie/ff679973%28v=vs.94%29.aspx
https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/some
Got the answer. i was doing silly mistake.
var filterData= _.filter(jsonData, function (data) {
return data.attrs.src;
});

Return Multidimensional Array

I want to return a multiply dimensional array from a function like so but I must be writing it wrong, I cant figure out whats wrong. I want to have key and value pairs.
function Multidimensional(){
return [
"one": [
"two":[],
"three":[
"testing.png":{source:"http..."}
],
"another.png": {source:"http..."}
];
}
If you want to have key/value pairs, you should use an object.
function Multidimensional(){
return {
"one": {
"two":[],
"three":{
"testing.png":{source:"http..."}
},
"another.png": {source:"http..."}
};
}
You can access the returned data like so:
var data = Multidimensional();
console.log(data['another.png']);
// or
console.log(data.one);

How to convert an object of objects to an array of objects?

I have an external file people.json. How I can convert it to a javascript array with json syntax?
this is the people.json content:
{
"1":{
"Name":"Jhon",
"Surname":"Kenneth",
"mobile":329129293,
"email":"jhon#gmail.com"
},
"2":{
"Name":"Thor",
"Surname":"zvalk",
"mobile":349229293,
"email":"thor#gmail.com"
},
"3":{
"Name":"Mila",
"Surname":"Kvuls",
"mobile":329121293,
"email":"mila#gmail.com"
}
}
I want an array with this format
var person = [
{ "name":"jhon" , "surname":"kenneth", "mobile":329129293, "email":"jhon#gmail.com"},
{ "Name":"Thor", "Surname":"zvalk", "mobile":349229293, "email":"thor#gmail.com" },
{ "Name":"Mila", "Surname":"Kvuls", "mobile":329121293, "email":"mila#gmail.com"}
];
I tried with the next code, but it doesnt worker:
var person;
$.getJSON('people.json', function (json) {
person[]= json
});
By the way, the file contacts.json is in my server.
Can use jQuery $.map()
var newArray=$.map( originalObject, function(item){
return item;
})
DEMO: http://jsfiddle.net/qmfn2/
Try like this:
$.getJSON('people.json', function (json) {
var people = [];
for (var key in json) {
if (json.hasOwnProperty(key)) {
var item = json[key];
people.push({
name: item.Name,
surname: item.Surname,
mobile: item.mobile,
email: item.email
});
}
}
// at this stage the people object will contain the desired output
});
First you will need to fetch the JSON file using an AJAX request. Then iterate through the received JSON object and add each property to an array.
function convertToArray (receivedObj) {
var array = [], key;
for (key in receivedObj) {
array.push(receivedObj[key]);
}
return array;
}
$.getJSON('people.json', function (json) {
var array = convertToArray(json);
});
Hope this helps!
Like this:
var array = $.map($.parseJSON(data), Object);
http://jsfiddle.net/mXFKL/
$.getJSON('people.json', function (json) {
var array = convertToArray(json);
});

Categories