I have the following problem:
var price = ['4','5','8','12']
var produce = ['kiwi','orange','apple','banana']
var stock = ['yes','no','no','yes']
i need to group them so that the end output is on array in the following format:
var store = [ ['4','kiwi','yes'],['5','orange','no'], ...]
im so confused as in how to make one array with these values into a 2d array. thanks
Using JavaScript with some overkill :):
var price = ['4','5','8','12']
var produce = ['kiwi','orange','apple','banana']
var stock = ['yes','no','no','yes']
// if the lengths/size of the above arrays are the same
var store = [];
for(var i = 0, len = price.length; i < len; i++) {
store.push([price[i], produce[i], stock[i]]);
}
// if the lengths/size of the above arrays aren't the same and you want the minimum full entries
var storeMin = [];
for(var i = 0, len = Math.min(price.length, produce.length, stock.length); i < len; i++) {
storeMin.push([price[i], produce[i], stock[i]]);
}
// if the lenghts/size of the above arrays aren't the same and you want the maximum entries with defaulting missing values to null
// replace the nulls by any default value want for that column
var storeMax = [];
for(var i = 0, pLen = price.length, prLen = produce.length, sLen = stock.length, len = Math.max(pLen, prLen, sLen); i < len; i++) {
storeMax.push([pLen>i?price[i]:null, prLen>i?produce[i]:null, sLen>i?stock[i]:null]);
}
var price = ['4','5','8','12']
var produce = ['kiwi','orange','apple','banana']
var stock = ['yes','no','no','yes']
var store = [];
$.each(price,function(ind,elm) {
store.push([elm,produce[ind],stock[ind]]);
});
console.log(store);
Related
i have object with array of names koktelData.strIngredient1 to 15 and values. I want loop that array and create new array with all values from koktelData.strIngredient1 to 15. I tray something like this but new array have 15 elements undefined1 to undefined15.
var sastojak = koktelData.strIngredient;
let sastojci = [];
for (let i = 1; i < 16; i++) {
var sastojak_niz = koktelData.strIngredient + [i];
const sastajak = sastojci.push(sastojak_niz);
}
console.log(sastojci);
try this out
let sastojci = [];
for (let i = 1, j = 0; i < 16; i++, j++) {
sastojci[j] = koktelData.strIngredient + i
}
console.log(sastojci);
If coktelData.strIngredient is an array then it should start at 0.
And this is how it should work. If not then contact me and add koktelData.strIngredient to the question so I can test it.
If koktelData.strIngredient is an String then pls show format
I am developing some Stock tools with AppScript and now I dont know why I cannot assign the array A values to array B. Could someone help me ?
I want to copy the values_array [l][0] -> dataArray [l][0]
Could you please give me the solution please?
//Filled up the array for MACD
// var dataArray = [["Stock","Date","Close Price","EMA12","EMA26","DIF","Signal"]];
var dataArray = [];
var accumulationEMA12 = 0;
for (var l = 0; l < values_array.length - 1; l++) // Get the previous day EMA12
{
var temp = values_array[l][5];
// var accumulationEMA12 = accumulationEMA12 + values_array[l][5];
var accumulationEMA12 = accumulationEMA12 + temp;
dataArray[l][0] = values_array[l][0]; // Fill Up the Stock No. <-Here
dataArray[l][1] = values_array[l][1]; // Fill UP the Date <-Here
if (l < 11) //if the dataset is < 12 rows or < 26 rows, calculate the average instead of EMA
{
}
}
//Filled up the array for MACD
// var dataArray = [["Stock","Date","Close Price","EMA12","EMA26","DIF","Signal"]];
var dataArray = [];
var accumulationEMA12 = 0;
for(var l = 0; l< values_array.length-1; l++) // Get the previous day EMA12
{
var temp = values_array[l][5];
// var accumulationEMA12 = accumulationEMA12 + values_array[l][5];
var accumulationEMA12 = accumulationEMA12 + temp;
dataArray[l][0] = values_array[l][0]; // Fill Up the Stock No.
dataArray[l][1] = values_array[l][1]; // Fill UP the Date
if(l < 11) //if the dataset is < 12 rows or < 26 rows, calculate the average instead of EMA
{
}
}
Values_array here
I think you are getting error at the line
dataArray[l][0] = values_array[l][0];
Because, no where I see you have initiated dataArray[l]. In your code, dataArray[l] is undefined, so when you try to do something like dataArray[l][1], you are essentially doing undefined[1] which will throw such type error
Uncaught TypeError: Cannot read property '1' of undefined
So what you need to do is, make sure dataArray[l] is an array itself. Something like this
var dataArray = [];
for (var l = 0; l < values_array.length; l++) {
// make sure dataArray[l] exists
dataArray[l] = [];
// do your operations
dataArray[l][0] = values_array[l][0];
dataArray[l][1] = values_array[l][1];
// other code.
}
Although, I would suggest using modern JS language features, like
const dataArray = [];
values_array.forEach((val, index)=> {
// notice that we are assigning a new array to dataArray[index]
dataArray[index] = [
val[0],
val[1],
];
// do other operations like
if (index < 11) {
// do stuff
}
});
Hope it helps.
So I'm turning a .csv file into an array of key-value pairs, I'm trying to print each unique value and i'm trying to figure out how I can check to make sure the values aren't identical. So for example:
var data = $.csv.toObjects(csv);
will turn everything into
[
{heading1:"value1_1",heading2:"value2_1",heading3:"value3_1",heading4:"value4_1",heading5:"value5_1"}
{heading1:"value1_2",heading2:"value2_2",heading3:"value3_2",heading4:"value4_2",heading5:"value5_2" }
]
I want to check if heading1 has the same value in both instances and if it does to only print the first instance of that value.
Convert your data into key–value pairs, where keys are the values from "heading1" like so:
var data = [
{heading1:"value1_1",heading2:"value2_1",heading3:"value3_1",heading4:"value4_1",heading5:"value5_1"},
{heading1:"value1_2",heading2:"value2_2",heading3:"value3_2",heading4:"value4_2",heading5:"value5_2" },
];
var filtered = {};
for (var i = 0, max = data.length; i < max; i++) {
var record = data[i];
if (!filtered[record.heading1]) {
filtered[record.heading1] = {};
}
filtered[record.heading1] = record;
}
var keys = Object.keys(filtered);
for (var i = 0, max = keys.length; i < max; i++) {
console.log(filtered[keys[i]]); // do print
}
How do I sort my product names in POS product list?
Here's my code, which says names.sort() is not a function.
In js file
for(var i = 0, len = this.product_list.length; i < len; i++)
{
var product_node = this.render_product(this.product_list[i]);
var names = this.product_list[i].display_name
var sorted_names = names.sort();
console.log('Sorted Names',sorted_names)
}
Do something like this,
var names=[];//array for names
for(var i = 0, len = this.product_list.length; i < len; i++)
{
var product_node = this.render_product(this.product_list[i]);
names.push(this.product_list[i].display_name);//push name in the array
}
names.sort();//sort names
console.log(names);//sorted array
I have two large arrays in my node application.
var styles = [{itemID:..., styleID:..., styleNum:..., otherFields...}]; // 42455 items
var products = [{productID:..., styleNum:..., otherFields...}]; // 72K items
I need to loop through the products and get the associated styleID from the styles array and add a new item into a new array. The styles array is sorted by styleNum. Here is what I have tried:
var i=0, len = products.length, items = new Array(products.length);
for (i = 0; i < len; i++)
{
var workingItem = products[i];
var styleID = filterStyles(workingItem.styleNum)[0].styleID;
var item = {styleID:..., other fields};
items[i]=item;
}
...
function filterStyles(styleNum)
{
var results = [];
var item;
for (var i = 0, len = createdStyles.length; i < len; i++)
{
item = createdStyles[i];
if (item.styleNum == styleNum) results.push(item);
}
return results;
}
This is very slow, it takes 1 second to iterate over 100 items from my products array. I tried the same using asyc.each, but get the same response time.
When I remove the filter function, it's lighting fast. Is there any way for me to improve my filter function?
To avoid scanning the array every time O(n2), you could create a map keyed by styleNum.
var styleNumMap = Object.create(null);
styles.forEach(function(style) {
if (!styleNumMap[style.styleNum]) {
styleNumMap[style.styleNum] = [];
}
styleNumMap[style.styleNum].push(style);
});
Then you can just do
var i=0, len = products.length, items = new Array(products.length);
for (i = 0; i < len; i++)
{
var workingItem = products[i];
var styleID = styleNumMap[workingItem.styleNum][0].styleID;
var item = {styleID:..., other fields};
items[i]=item;
}