Referencing a javascript object literal array - javascript

How would you reference the models (Accord, CRV, Prius, etc) in this structure?
Is this a bad structure to be able to extract the makes...then use a make to get the models...then use the model to get the options?
var cars = [
{
"makes" : "Honda",
"models" : [
{'Accord' : ["2dr","4dr"]} ,
{'CRV' : ["2dr","Hatchback"]} ,
{'Pilot' : ["base","superDuper"] }
]
},
{
"makes" : "Toyota",
"models" : [
{'Prius' : ["green","reallyGreen"]} ,
{'Camry' : ["sporty","square"]} ,
{'Corolla' : ["cheap","superFly"] }
]
}
];
Thanks

The structure:
var cars = [
{ name: 'Honda', models: [
{ name: 'Accord', features: ['2dr', '4dr'] },
{ name: 'CRV', features: ['2dr', 'Hatchback'] },
{ name: 'Pilot', features: ['base', 'superDuper'] }
]},
{ name: 'Toyota', models: [
{ name: 'Prius', features: ['green', 'superGreen'] },
{ name: 'Camry', features: ['sporty', 'square'] },
{ name: 'Corolla', features: ['cheap', 'superFly'] }
]}
];
I wrote about the traversal and everything else here.

cars[0].models.Accord
cars[0].models.CRV
cars[0].models.Pilot (See olliej's answer)
Though, it may be easier to use the following access concept:
cars.Honda.Accord
cars.Toyota.Prius
...using...
var cars = {
Honda : {
Accord : ["2dr", "4dr"],
CRV : ["2dr", "Hatchback"],
Pilot : ["base", "superDuper"]
},
Toyota : {
Prius : ["green", "reallyGreen"],
Camry : ["sporty", "square"],
Corolla : ["cheap", "superFly"]
}
};

Jonathan's is correct, but he missed the additional level of Array's at the model level, so it should be
cars[0].models[0].Accord
cars[0].models[1].CRV
etc
I suspect you would find it easier to use a structure along the lines of:
var cars = [
{makes : "Honda",
models : {
Accord : ["2dr","4dr"],
CRV : ["2dr","Hatchback"],
Pilot: ["base","superDuper"]
}
},
{makes :"Toyota",
models : {
Prius : ["green","reallyGreen"],
Camry : ["sporty","square"],
Corolla : ["cheap","superFly"]
}
}];
In which the models array is replaced by an object (or associative array if you like)
[edit (olliej): tidying up code in second example]

You can traverse models with this code:
for (var i = 0, carslen = cars.length; i < carslen; i++) {
for (var j = 0, modelslen = cars[i].models.length; j < modelslen; j++) {
// do something with cars[i].models[j]
}
}
but I agree with Olliej about changing the structure of your JSON to his format.

If I were you, I wouldn't lump all your data into one big multidimensional array/object literal mess like that. I'd encapsulate each object and use methods to access the data. It'll mess with your brain a lot less.

Related

delete array field in a mongodb database

I am using mongoDB and my JSON (table name is "student") looks like
[{
name : "John",
subjects:["English", "Maths"]
},
{
name : "Winsel",
subjects : ["Maths"]
}]
here "subjects" is some other table and associated with student, I able to get data by using find query,
student.find({}).populate('subjects').then(function(studentData){console.log(studentData)})
by using this query i can get Data as
[{
name : "John",
subjects:["English", "Maths"]
},
{
name : "Winsel",
subjects : ["Maths"]
}]
after that i need to delete the field "subjects" which length is less than 2, I am using
if(studentData.subjects.length < 2){
delete studentData.subjects
}
but it doesn't delete the field, it shows the same result when i not using the if condition.
my expected JSON is
[{
name : "John",
subjects:["English", "Maths"]
},
{
name : "Winsel"
}]
Can any one guide me, Thanks in advance
Try this
var studentsList = [{
name : "John",
subjects:["English", "Maths"]
},
{
name : "Winsel",
subjects : ["Maths"]
}];
var newStudentList = _.map(studentsList , function(eachStudentData){
if(eachStudentData.subjects <2){
return _.pick(eachStudentData, 'name');
} else {
return eachStudentData;
}
});
You can use the newly formed students list newStudentList for your code
To remove the fields in your json response, you can use delete inside the callback function of your query function.
student.find({}).populate('subjects').then(function(studentData){
var newData = studentData;
newData.map(item => {
if(item.subjects.length < 2) {
delete item.subjects;
}
return item;
})
// you can use the newData here;
})
This works to me. I don't know if you are working with a JSON object and you need to transform it to javascript object (students.toObject()).
var students = studentsJSON.toObject();
var newStudents = students.map(studentData => {
if(studentData.subjects.length < 2) {
delete studentData.subjects;
}
return studentData;
});
try this code
var data = [{
name: "John",
subjects: ["English", "Maths"]
},
{
name: "Winsel",
subjects: ["Maths"]
}
];
console.log(data);
for (var i = 0; i < data.length; i++) {
if (data[i].subjects.length < 2) {
delete data[i].subjects;
}
}
console.log(data);
I don't know what are you trying to achieve here but populate works on application level . you can directly use aggregation pipeline for this using $project(or similar $addFields) and $lookup
db.students.aggregate(
[
{
$addFields: {
subjects:{
$cond:[{$gte:[{$size:"$subjects"},2]},"$subjects",null]
}
}
},
{
$lookup: {
"from" : "subjects",
"localField" : "subjects",
"foreignField" : "name",
"as" : "subjects"
}
},
]
);
from mongodb 3.6 you can use "$$REMOVE" instead of null in addFields stage
and also after $lookup you can use $project again to remove empty array

Find a Value in a Dictionary with a certain Property

Say I have a dictionary like
objDict = {
id1 : { name: 'someObj1', id: 'someId1' },
id2 : { name: 'someObj2', id: 'someId2' },
id3 : { name: 'someObj3', id: 'someId3' },
}
If I wanted to search for the property "someId2" of the "id" property in Values of that dictionary.. how would I be able to grab the the whole object after finding it?
I really can't think of a good way to do it outside of iterating the dictionary with a for-in loop. I was thinking of using Object.values() but even then I can't think of a way of using that to grab the whole object that contains someId2.. I would only be able to determine if that property existed.
I was just wondering if there was a better way than a for-in loop. Thanks
you can just get all the keys using Object.keys(), then iterate over these and select the object you want based on the required id.
var objDict = {
id1 : { name: 'someObj1', id: 'someId1' },
id2 : { name: 'someObj2', id: 'someId2' },
id3 : { name: 'someObj3', id: 'someId3' },
};
var obj;
Object.keys(objDict).forEach(x => obj = objDict[x].id === 'someId2' ? objDict[x]: obj);
console.log(obj);
There is Object.entries(), but for-in loop is more efficient than the other alternatives.
objDict = { id1 : { name: 'someObj1', id: 'someId1' },
id2 : { name: 'someObj2', id: 'someId2' },
id3 : { name: 'someObj3', id: 'someId3' } }
item = Object.entries(objDict).find(a => a[1].id === 'someId2')
console.log(JSON.stringify(item))
The following code snippet containing break; statement could statistically provide a better performance vs. the selected one.
objDict = {
id1 : { name: 'someObj1', id: 'someId1' },
id2 : { name: 'someObj2', id: 'someId2' },
id3 : { name: 'someObj3', id: 'someId3' },
};
var obj;
for (var key in objDict)
{
if (objDict[key].id==='someId2')
{
obj = objDict [key];
break;
}
}
console.log(obj);
ECMA Script 2015 provides another alternative of using Map object as described in : How to iterate (keys, values) in javascript?
Hope this may help.
This is a generic indexing problem, so no, there's no good way to do this other than looping through each value in the dictionary. This is because you've created a data structure to access the data quickly (O(1) time), but the drawback is that if you want to find the data through another index quickly, you'll have to search manually O(n) time for it.
If you really did care about the actual value of someId, then the canonical answer would be to create 2 separate dictionaries w/ the same data w/ different key values. This is very similar to having 2 indexes on a database table.

How to create an array as such using javascript

Using javascript, I want to create an array that has the structure so that it can have all states within the country array. Each state would be an array consisting all the regions within it. And each region would contain all the health clubs within it. Something like this:
[Australia]=>array([NSW]=>array([Sydney]=>array(element1, element2, ...)));
So far I have tried many things including the code below:
$('#search-widget-data li').each(function(){
var dataElement = $(this);
searchWidgetData.push(dataElement.text());
alldata.push([dataElement.prevAll(".state:first").text(),
dataElement.prevAll(".region:first").text());
});
You can use a form like this:
var countries = [
{
name: "Australia",
states: [
{
name: "state1",
regions: [
{
name: "region1",
healthclubs: [
"healthclub1", "healthclub2"
]
},
{
name: "region2",
healthclubs: [
"healthclub1", "healthclub2"
]
}
]
},
{
name: "state2",
regions: [
{
name: "region1",
healthclubs: [
"healthclub1", "healthclub2"
]
}
]
}
]
},
{
name: "USA"
}
];
You may want to store your data structure in the JSON format which will make it easy for storage and working with as a JavaScript object.
You may try this one out.
var regArr[n1][n2];
but you need to specify the length of each inner array.

Stuck implementing a nested data filtering loop, possibly a more elegant way of doing this?

Here's the data. What I want to do is inject all this data into one variable that contains all the categories and all the books belonging to those categories:
var categories = [
// ...
{
"_id" : "5436b12b456f61180f815c06",
"name" : "Horror"
},
{
"_id" : "5436b43f18bbc8a8073e786e",
"name" : "Action"
}
// ...
];
var books = [
// ...
{
"_id" : "5436b43f18bbc8a8073e786d",
"title" : "The Bourne Legacy",
"category" : "5436b43f18bbc8a8073e786e"
},
{
"_id" : "5436b5c9de9884e010d5ef24",
"title" : "Digital Fortress",
"category" : "5436b50dc6faf3d41071a669"
}
// ...
];
And this is a mockup of how I'd like the data to look like after filtering:
categories_titles = [
{
categoryName: 'Category One',
categoryTitles: [
{title: 'Book 1'},
{title: 'Book 2'},
// ...
]
},
{
categoryName: 'Category Two',
categoryTitles: [
{title: 'Book 3'},
{title: 'Book 4'},
// ...
]
}
];
And this is how I'm trying to solve the problem:
categories_titles= [];
for (var category in categories) {
for (var book in books) {
if (books[book].category == categories[category]._id) {
categories_titles.push(
{
name: categories[category].name,
titles: [
{title: books[book].title}
]
}
);
}
}
}
This method works only partially; all the categories are being listed but some are being repeated, and some titles are missing or otherwise not where they're supposed to be.
Also, accessing items, something like: categories_titles[0].titles[0].title is quite a monstrosity and I have a feeling that this could be solved more elegantly.
Please advise.
There is indeed a much better way to do this. I'm going to rearrange your data structure. The filter to go from my data structure to yours is a linear time algorithm, though I don't see why you'd want to. But if you must, the translation is linear time.
result = {};
for (var category in categories) {
result[category._id] = {name : category.name, books: []};
}
for (var book in books) {
//Probably also want some safety logic for if result[book.category] is undefined...
addBook(result[book.category].books, book); //you already know how to add your book info, so I won't duplicate that logic.
}
Afterwards you can either return result, or do logic to change result into your mocked up data structure. But creating this preliminary structure will save you computation time, because dictionary accesses are MUCH faster than iterating over your arrays multiple times.
If b = number of books and c = number of categories.
Complexity Your algorithm: O(b * c)
Complexity My algorithm: O(b + c)
Complexity My algorithm with translation to your data structure O(2 * (b + c)) though the memory complexity goes up. A good tradeoff for this problem, in my opinion. O(b*c) is not very good.

Creation of a multi-level JSON string

I want to create a multi-level JSON string with JS.
Scenario
3 countries with 5 grandfathers with 3 kids which whom also have 3 kids that have 5 friends.
I get the data from a external JSON file that looks like this.
{"countries":[
{
"name":"USA",
"grandfathers":[
{
"gFName":"Steve",
"grandfathersKid":[
{
"gFKName": "Linda",
"kid": [{
"name": "Steve JR",
"friends": [{
"name": "Kriss|John|Martin|Steven"
}]
}
]
}
]
}
]
}
]}
And now I want to store some of the countries with people and their relatives and friends in a a new JSON list that looks exactly as the list made in the external json file. I aim to use this "homemade" list later on in the script.
My initial response for this was
var tree = new Array();
tree = {};
var countries = new Array();
countries[0] = "canada";
countries[1] = "USA";
countries[2] = "Mexico";
countries[0][0] = "Steve"; //Lives in Canada
countries[0][0][0] = "Linda"; //Daughter of Steve
countries[0][0][0][0] = "Steve JR"; // Kid of Linda
countries[0][0][0][0][0] = "Kriss"; //Steves Friend
...
$.each(countries...function(index, value){
tree[index].country = value;
$.each(grandfathers...function(key, value){
tree[index].country[key].grandfather = value;
}
And so on, but this is not giving me the result I want. What am I doing wrong? And a more effective way than to take each of everything?
Third edit...
Is this the sort of thing you're trying to do?
var countries = $.map(oldCountries || [], function(country) {
return {
name: country.name,
people: $.map(country.grandfathers || [], function(gpa) {
return {
name: gpa.gFName,
children: $.map(gpa.grandfathersKid || [], function(parent) {
return {
name: parent.gFKName,
children: $.map(parent.kid || [], function(kid) {
return {
name: kid.name,
friends: kid.friends
};
})
};
})
};
})
};
});
I wasn't sure what to do with the friends node. Should that be normalized into something more useful, or do you want to leave it alone?
This Fiddle demonstrates the technique.
I think we'd need to know more about your requirements. But several thing I see here are:
You declare tree and initialize it as an Array, then immediately reinitialize it as an
empty object
You are not creating the intermediate nodes here, such as tree[index] but just assuming
that they exist.
You are trying to assign the country[key] property of an object, using the dot-property
access.
Can you supply the countries structure and the grandfather's structure. And are they nested?
And finally, what would you like for the output format? The code above hints at it, but it's still a little fuzzy.
Edit
So are you trying to achieve a structure something like this?:
var countries = [
{
name: "Canada",
people: [
{
name: "Steve",
children: [
{
name: "Linda",
children: [
{
name: "Steve, Jr.",
friends: [
{
name: "Kriss"
}
//, more friends
]
}
//, more grandchildren
]
}
//, more parents
]
}
//, more grandparents
]
}
//, more countries
];
May be this jsfiddle can help you to get started?
And here is an example derived from your code.
Sounds like a homework, so I'll try to point you in the right direction. I think you are confusing objects and arrays. You could use a "country" object and a "person" object. A country object should have an array of person objects, as inhabitants. Person objects can have an array of person objects as descendants. Add a method like "addDescendant", which creates a new person under a person. From There you can build the structure as you like. Here is some pseudo code:
countries = [];
function Country(name){ this.name = name; this.population = [];}
function Person(kids){this.descendants = []; this.addDescendant = function(){...};
//loop from 1 to kids and add descendants as "new Person"
}
person = new Person(3);
country1 = new Country("MyCountry1");
// now add people to country1.population
countries.push(country1);
The final structure should look something like this:
countries = [
{ name: "country 1",
people: [{ name: "Steve"},
{name: "Clara", descendants: [{name: "Clara's daughter"},
{name: "Clara's son"}]
]}
},
{ name: "country 2",
people: [{}, {} ...]
}
];

Categories

Resources