complicated custom sorting in nested array - javascript

I've problem sorting my nested array, says I've json like this
var orders = [{
'orderId': 1,
'sales': [{
'salesNumbers': 3
}]
}, {
'orderId': 2,
'sales': [{
'salesNumbers': 4
}]
}];
and I wish I can sort orderId base on salesNumbers. You may say it's impossible or I made a mistake by putting sales as array but it contain only 1 object which is salesNumbers. That's not a mistake, I just do not want to simplify my problem.
so it's possible to, without changing the data structure, sort orderId base on salesNumbers?? My app demo http://jsfiddle.net/sq2C3/

Since you say the sales array only has one item in it, you can order by salesNumbers like this:
orderBy:'sales[0].salesNumbers'
Here is an update of your fiddle: http://jsfiddle.net/wittwerj/sq2C3/2/

Related

Getting a value from the 2 dimensional array in Javascript

In this program I am supposed to create an 2 dimensional array such as ["S1","S2","S3","S4"] AND ["John","Ben","Dan","Jim"] and give the name as output when the specified serial no is given as input. Eg. John will be the output of S1.I was able to create the program using objects but I am unable to do it with arrays. I dont know how to create a 2 dimensional array as well. Kindly help.
Thanks.
Assuming you mean nested arrays and the result you are after is:
[ [ 'S1', 'S2', 'S3', 'S4' ], [ 'John', 'Ben', 'Dan', 'Jim' ] ]
Consider the following:
var mainArray = [];
var arr1 = ["S1","S2","S3","S4"];
var arr2 = ["John","Ben","Dan","Jim"];
mainArray.push(arr1, arr2);
This should give you the result you are after. Please keep in mind that your question is a bit vague and doesn't tell us what you have tried. It sounds like you need some practice with basic JavaScript. I suggest finding tutorials online(which there are more than enough) and working through them.
For future reference, be sure to show what you have tried in your question.

Angularjs - How to traverse, add and delete elements in nested arrays

I have a nested array:
$scope.audits = [{
name: '2015',
sections:[{
section:'mm',
id:'1',
subsections: [{
subsection:'aa',
mainelements: ['cc','dd']
},{
subsection:'bb',
mainelements: ['ee','ff','gg']
}]
}]
}];
I want to have a few buttons to add and delete nested elements at will. I cannot use $index because it is multiple layers of array.
Here is my Plunker
This problem got it's crazy solutions out here: AngularJS: traversing nested arrays . I'm not going to rewrite the solutions once again because it's unefficient. Don't forget to use search before asking a question.

Javascript function to count nested array of object properties possibly using reduce/map

I have a problem and I have looked at a few reduce questions on here and I just can't seem to wrap my head around a way to do this. I have an array of objects. Each object has the same properties, about 30 of them. Each of the properties has completely different types of information. I want to be able to create a new object/array easily. My example is below.
var array = [{parts: 12345, purchased: "yes", customerId: 12541},
{parts: 12432, purchased: "no", customerId: 55514},
{parts: 12345, purchased: "Yes", customerId: 44421}];
What I want to do is find a way to extract useful data depending on the type of information in the array. For example:
some_function(array) {
...
return { parts: {uniquePart: 12345, timesListed: 2}};
}
}
I may also want to extend that returned object and count the number of times purchased was either yes or no. I have tried numerous approaches but I am thinking this is more a data model issue than a programming issue.
I am parsing this data off of strings of receipts. Once the plain text string is parsed I get a large 30 property object for each transaction. Some of the properties also are nested objects or arrays as well.
I want to correlate data across multiple transactions. Maybe I need to research a better way to approach this type of situation as a whole.
So I understand the question is a little vague but what I really want to know is what is the best way with the array given to end up with the following data structure:
{parts: {uniquePart: 12345, timeListed 2}}
I believe once I understand how to itterate through the nested array of objects and build the new object I can go from there. My current attempts using reduce have not yielded fruit.
array.reduce(acc,obj){return This is where I am a little lost}
This solution features Array.prototype.forEach, Object.keys and Array.prototype.map for a temporary object count and returns the wanted array with one object for every part.
function getCount(array) {
var count = {};
array.forEach(function (a) {
count[a.parts] = (count[a.parts] || 0) + 1;
});
return Object.keys(count).map(function (k) {
return { parts: { uniquePart: k, timesListed: count[k] } };
});
}
var array = [{ parts: 12345, purchased: "yes", customerId: 12541 }, { parts: 12432, purchased: "no", customerId: 55514 }, { parts: 12345, purchased: "Yes", customerId: 44421 }];
document.write('<pre>' + JSON.stringify(getCount(array), 0, 4) + '</pre>');

Couchdb view to selecting products using another key

I' ve a big list of products (they are ink cartidges and toner) stored in a couchdb, for every document i've got several fields, and one particular field called "models" that is a multidimensional array like this:
"models": {
"Brother": {
"HL": [
"5200",
"5240",
"5240 DN",
"5240 DNLT",
"5240 L",
"5250 DN",
"5250 DNHY",
"5250 DNLT",
"5270 DN",
"5270 DN 2 LT",
"5270 DNLT",
"5280 DW",
"5280 DWLT"
]
},
"": {
"MFC": [
"8460 DN",
"8460 N",
"8860 DN",
"8860 N",
"8870 DW"
],
"DCP": [
"8060",
"8065 DN"
]
},
"Lenovo": {
"": [
"LJ 3500",
"LJ 3550",
"M 7750 N"
]
}
},
I've to do several things with this data, starting with fixing a little problem that i've got while they was written, if you look at the example that i've posted, the second and the third brother serie has an empty array string instead of the "brand" value that should be "Brother"... i've many records that miss the "brand" key for certain series, and this should be set to the previous serie brand, i.e. the "MFC" serie should has the top level key set to "Brother" that is the key of the previous serie.
I can do this using a view in couchdb?
After doing that i need to obtain a list of unique models using another view with every product associated to them.. in other words i've to select all of my products using the last level of the multidimensional array "models" as key (including also brand and serie values as secondary result for futher sorting and filtering) and all the products that contain that certain model as value.
An output like this:
key: "Brother, HL, 5200" - value: "id_product1, id_product2, idproduct3, etc."
Before i start to reading all documentation present on the earth can someone explain me if this thin is at least doable?
Thanks in advance...

Highcharts breaks up my data array in a strange way

I'm using Highcharts (link to a specific demo -- click View Options to compare below code) to render some data for personal-use.
[Note: this is almost definitely a me-related error, so I don't mean to insinuate that Highcharts is at fault -- it's really a wonderful api!]
When setting the series.data field of the chart object, I encounter strange array sectioning.
For instance, this works fine:
series: [{
name: 'seriesName',
data: [22, 24, 15]
}]
And three data points are plotted with values 22, 24 and 15.
However,
series: [{
name: 'sillySeries',
data: chartData[0]
}]
renders 3 points with the values 2, 4 and 5 and the titles of the points are 2, 2 and 1 respectively... that is, it's splitting the array entries into a title and then a value for two-digit numbers.
console.log shows me that chartData looks like
22,24,15,2010-9,2010-10,2010-11
(3 values for 3 months)
Although the actual arrays are more like [22,24,15][2010-9,2010-10,2010-11]
My 2d-array chartData[][] has [column][row] indices for the data, and I figured that asking for chartData[0] would return an array.
Perhaps my question should be: How do I return a 1d array from a 2d array representation for setting properties/values in javascript objects? It seems like I'm probably overlooking something very obvious.
edit:
data: [24]
plots one point at 24, awesome.
data: '24'
splits up the data as title and then value, effectively plotting a point at 4 and not 24.
So the representation becomes a string when I try to pass an array to the data field. Any way I can do like string.toArray ?
edit 2:
chartData[col][row] is declared as an empty array
var chartData = [];
and subsequently filled up with values by iterating over an HTML table's rows and columns, where the innermost loop contents look like
chartData[cellIndex][rowIndex] = $(currentCell).text();
While chartData is probably an array, it sounds like chartData[0] is a string. When you use an array index on a string you get the character at that position, which is exactly what you are experiencing...
renders 3 points with the values 2, 4 and 5 and the titles of the points are 2, 2 and 1 respectively... that is, it's splitting the array entries into a title and then a value for two-digit numbers.
There are many ways to define arrays within arrays in javascript; here is one:
var chartData = [
[22,24,15]
,['2010-9','2010-10','2010-11']
];
parseInt() saved the day.
something like
for (i=0; i<chartData[0].length; ++i) {
columnOne.push(parseInt(chartData[0][i]);
}
gave me an array that I could then use to display the data with the data field in the object notation
data: columnOne
or more literally
chart1.series[0].data = columnOne;
Thank you James for pointing out
While chartData is probably an array, it sounds like chartData[0] is a string.
which lead to my discovery that the data in my array was actually all strings, which I kept this way because some fields are strings, some are dates (YYYY-MM-DD) and some are just ints.
Many thanks to you both =)
Well, this is not a real answer, but might help you.
Highcharts enables you to preprocess certain options. In your case, you could do something like :
options.series[0].data = new Array(1, 0, 4);
doc reference : http://www.highcharts.com/documentation/how-to-use#options
If this fails, comment above.

Categories

Resources