javascript multidimensional associative array - javascript

So I have
var arrays = [
[ Material A, 1, 2, 3, 4, 5 ],
[ Material B, 6, 7, 8, 9, 10 ],
[ Material C, 11, 12, 13, 14, 15 ]
];
and I need to put it in this format which is a multidimensional associative array,
var bigdata = [
{ "Name": "MaterialA", "Row1": 1, "Row2": 2, "Row3": 3, "Row4": 4, "Row5": 5 },
{ "Name": "MaterialB", "Row1": 6, "Row2": 7, "Row3": 8, "Row4": 9, "Row5": 10 },
{ "Name": "MaterialC", "Row1": 11, "Row2": 12, "Row3": 13, "Row4": 14, "Row5": 15 }
];
I am trying
var bigdata = new Array(3);
for (i=0; i<3; i++)
{
// do name
bigdata[i][0] = {"Name" : arrays[i][0]};
for (j=1; j<6; j++ )
{
// rest of that row
}
}
But so far it is not working when I try to store the first "Name": "MaterialA" . What am I doing wrong or can this even be done? Thanks for the help.

This is working for me. Notice I removed the [0] from your bigdata[i][0], and added the row assignment code to your "j" loop.
for (i=0; i<3; i++)
{
// do name
bigdata[i] = {"Name" : arrays[i][0]};
for (j=1; j<6; j++ )
{
// rest of that row
bigdata[i]['Row' + j] = arrays[i][j];
}
}
JSFiddle: http://jsfiddle.net/ub54S/1/

The proper way to set a property of an associative array/object is like this:
bigdata[i]["Property"] = value // this allows dynamic property name
bigdata[i].Property = value // or like this, if property is hard-coded
So in your case, it should be:
bigdata[i] = {} // initialize a blank object
bigdata[i].Name = arrays[i][0];
for ( j=1; j<6; j++ )
bigdata[i]["Row" + j] = arrays[i][j];
Here is a demo: http://jsfiddle.net/56tk5/

Related

I want to create the clusters of similar strings in the flowing array

I have the array like
[
"/core/api/v2.0/wallet/62da5521930eaf0f2e855": 1,
"/core/api/v2.0/wallet/62da382894c5dd0f9ab11": 1,
"/core/api/v2.0/users/sync/603a9c6a-c686-42af-b5f7-1f5bcfd75": 1,
"/core/api/v2.0/users/sync/64c73c5b-e6cc-4f98-9c54-69e7c796a": 3,
"/core/api/v2.0/users/sync/c3dfe00d-3950-401b-b068-d64aac99f39c": 3,
"/core/api/v2.0/users/sync/32cd70c8-ca92-4075-bb2f-1b9d99527723": 2,
"/core/api/v2.0/users/sync/245b0029-c05b-4007-bacb-656db21c170e": 1,
"/core/api/v2.0/users/sync/9175d6a9-386c-4b0d-9db4-c6053913c354": 1,
"/core/api/v2.0/tools/generate/trigger-download": 22,
"/core/api/v2.0/tools/generate/search-photos": 17,
]
I want the results like
[
"/core/api/v2.0/wallet/:id": 2,
"/core/api/v2.0/users/sync/:id: 11 "
"core/api/v2.0/tools/generate/:tool-name": 39,
]
just loop over array and keep counter if meets specific match likewise:
let wallet_match=0;
for (let i = 0; i < your_array.length; i++) {
if(your_array[i].includes("/core/api/v2.0/wallet/")) {
wallet_match+=1;
}
}
analogically you can list for other matches as well!

How I can assign a custom format to an array?

I have an array of dates that I use for mapping. When trying to pass items from a different array (names) using the index, I get undefined values because the "names" array has less entries than the index (dates).
I have a third array with what should be the correct format (format):
let names = [
"chair",
"table",
"door",
"window",
"glass",
"wine",
"car",
"keys",
"dream",
"keyboard",
"vodka",
"pepsi",
"bag",
"ikea",
"mercedes",
"soprano"
];
let format = [3, 3, 1, 5, 4, 4, 3, 5, 13, 10, 3, 5, 5, 2, 2, 10];
let dates = [
"2021-10-04T04:00:00.000Z",
"2021-10-05T04:00:00.000Z",
"2021-10-06T04:00:00.000Z",
"2021-10-13T04:00:00.000Z",
"2021-10-14T04:00:00.000Z",
"2021-10-15T04:00:00.000Z",
"2021-10-15T04:00:00.000Z",
"2021-10-17T22:00:00.000Z",
"2021-10-18T22:00:00.000Z",
"2021-10-19T22:00:00.000Z",
"2021-10-20T22:00:00.000Z",
"2021-10-21T22:00:00.000Z",
"2021-10-17T22:00:00.000Z",
"2021-10-18T22:00:00.000Z",
"2021-10-19T22:00:00.000Z",
"2021-10-20T22:00:00.000Z",
"2021-10-19T04:00:00.000Z",
"2021-10-20T04:00:00.000Z",
"2021-10-21T04:00:00.000Z",
"2021-10-22T04:00:00.000Z",
"2021-10-19T04:00:00.000Z",
"2021-10-20T04:00:00.000Z",
"2021-10-21T04:00:00.000Z",
"2021-10-25T04:00:00.000Z",
"2021-10-26T04:00:00.000Z",
"2021-10-27T04:00:00.000Z",
"2021-10-28T04:00:00.000Z",
"2021-10-29T04:00:00.000Z",
"2021-10-25T04:00:00.000Z",
"2021-10-26T04:00:00.000Z",
"2021-10-27T04:00:00.000Z",
"2021-10-28T04:00:00.000Z",
"2021-10-29T04:00:00.000Z",
"2021-11-01T04:00:00.000Z",
"2021-11-02T04:00:00.000Z",
"2021-11-03T04:00:00.000Z",
"2021-11-04T04:00:00.000Z",
"2021-11-05T04:00:00.000Z",
"2021-11-08T04:00:00.000Z",
"2021-11-09T04:00:00.000Z",
"2021-11-10T04:00:00.000Z",
"2021-11-01T04:00:00.000Z",
"2021-11-02T04:00:00.000Z",
"2021-11-03T04:00:00.000Z",
"2021-11-04T04:00:00.000Z",
"2021-11-05T04:00:00.000Z",
"2021-11-08T04:00:00.000Z",
"2021-11-09T04:00:00.000Z",
"2021-11-10T04:00:00.000Z",
"2021-11-11T04:00:00.000Z",
"2021-11-12T04:00:00.000Z",
"2021-11-11T04:00:00.000Z",
"2021-11-12T04:00:00.000Z",
"2021-11-13T04:00:00.000Z",
"2021-11-15T04:00:00.000Z",
"2021-11-16T04:00:00.000Z",
"2021-11-17T04:00:00.000Z",
"2021-11-18T04:00:00.000Z",
"2021-11-19T04:00:00.000Z",
"2021-11-16T04:00:00.000Z",
"2021-11-17T04:00:00.000Z",
"2021-11-18T04:00:00.000Z",
"2021-11-19T04:00:00.000Z",
"2021-11-20T04:00:00.000Z",
"2021-11-23T04:00:00.000Z",
"2021-11-24T04:00:00.000Z",
"2021-11-23T04:00:00.000Z",
"2021-11-24T04:00:00.000Z",
"2022-01-05T04:00:00.000Z",
"2022-01-06T04:00:00.000Z",
"2022-01-07T04:00:00.000Z",
"2022-01-10T04:00:00.000Z",
"2022-01-11T04:00:00.000Z",
"2022-01-12T04:00:00.000Z",
"2022-01-13T04:00:00.000Z",
"2022-01-14T04:00:00.000Z",
"2022-01-17T04:00:00.000Z",
"2022-01-18T04:00:00.000Z"
];
let app_multiple = dates.map(function combineTitleData(dataItem, index) {
return {
text: 'LR' + dates[index] + ': ' + names[index],
};
});
console.log(app_multiple);
What I would like to achieve is using the "format" array is to follow the patern and use this array to construct the mapping.
The format contains: [3, 3, 1, 5, 4, 4, 3, 5, 13, 10, 3, 5, 5, 2, 2, 10];
I would like to use in the mapping the "names" array like this:
(chair) -> 3 times
(table) -> 3 times
(door) -> 1 time
(window) -> 5 times
(glass) -> 4 times
.... etc
So the output would be like this:
[
{
"text": "LR2021-10-04T04:00:00.000Z: chair"
},
{
"text": "LR2021-10-05T04:00:00.000Z: chair"
},
{
"text": "LR2021-10-06T04:00:00.000Z: chair"
},
{
"text": "LR2021-10-13T04:00:00.000Z: table"
},
{
"text": "LR2021-10-14T04:00:00.000Z: table"
},
{
"text": "LR2021-10-15T04:00:00.000Z: table"
},
{
"text": "LR2021-10-15T04:00:00.000Z: door"
},
{
"text": "LR2021-10-17T22:00:00.000Z: window"
},
{
"text": "LR2021-10-18T22:00:00.000Z: window"
},
{
"text": "LR2021-10-19T22:00:00.000Z: window"
},
{
"text": "LR2021-10-20T22:00:00.000Z: window"
},
{
"text": "LR2021-10-21T22:00:00.000Z: window"
}
.... etc
.... etc
]
Is this possible to do? Thanks.
The first option is:
var numTimesUsed = 0;
var nameIndex = 0;
let app_multiple = dates.map(function combineTitleData(dataItem, index) {
if(format[nameIndex] == numTimesUsed) {
nameIndex++;
numTimesUsed = 0;
}
numTimesUsed++;
return {
text: 'LR' + dates[index] + ': ' + names[nameIndex],
};
});
The second option is (I'm looping through the names map instead of dates for this option):
var dateIndex = 0;
let app_multiple = names.map(function combineTitleData(nameItem, index) {
var datesForName = [];
for(var i = 0; i < format[index]; i++) {
datesForName[i] = {
text: 'LR' + dates[dateIndex] + ': ' + names[index],
};
dateIndex++;
}
return datesForName;
});
You could probably also use some math to avoid extra variables in the second option, but its easier to just have an extra variable.

Converting relationships between data inside a JavaScript Array

I have got a function that produces an array that is made up of X amount of sub-arrays containing Y amount of objects. Both of these factors are passed to a function to produce an array that looks something like this:
[
[ { '0': 3 }, { '1': 4 }, { '2': 6 }, 'Estimate:': '0jvyt8a' ],
[ { '0': 4 }, { '1': 6 }, { '2': 3 }, 'Estimate:': 'mc973fs' ],
[ { '0': 4 }, { '1': 1 }, { '2': 3 }, 'Estimate:': 'vwsfh8k' ],
[ { '0': 4 }, { '1': 3 }, { '2': 5 }, 'Estimate:': 'n6xzge3' ],
[ { '0': 8 }, { '1': 7 }, { '2': 1 }, 'Estimate:': 'v0jn7bh' ]
]
My question is, is there a way I can convert this array from this structure. To a structure shown below:
[
[1,{1: "vwsfh8k"}, {2: "v0jn7bh"}]
[3,{1: "0jvyt8a"}, {2: "mc973fs"}, {3:"vwsfh8k"}, {4:"n6xzge3"}]
]
Basically, my aim is to take the original array generated by the script (see below) and pass it through another function to record how many times each number was present and what it's 'estimate' number was.
In this example, I just created random numbers between 0 and 10 so an option would be to iterate and count each value I guess but unfortunately, I can't do this because eventually I will be using 5-letter combinations instead of numbers but numbers were easiest to show for an example and proof of concept.
So, I guess, I need to get an array of each unique value and then look at each value up in the original array to find out what estimate IDs have it present. Unfortunately, I don't have even an idea of where, to begin with, this, so I was hoping you guys can help.
Code to generate random array:
// Making an empty array
const arr = [];
//Generating the estimate IDs and placing them all in their own object in their own array.
function estimateGen(length, nodes) {
for (var i = 0; i < length; i++) {
const estimate = [];
let estimateVal = Math.random().toString(36).replace('0.','').slice(0,7);
estimate[`Estimate:`] = estimateVal;
arr.push(estimate);
nodeGen(estimate, nodes)
}
}
// Adding x amount of nodes between 1 and 10 into each estimate sub-array in their own objects.
function nodeGen(estimate, nodes) {
for (var i = 0; i < nodes; i++) {
const node = {};
let nodeID = Math.floor(Math.random() * 10) + 1;
node[i] = nodeID;
estimate.push(node);
}
}
// Calling the function and saying how many nodes per estimate we want.
estimateGen(5, 3);
console.log(arr);
If you have any suggestions on how to improve this code or as to why the estimate values in the sub-array are always last in the array that would be very helpful.
Thank you
--- EDIT ---
I have changed the code that generates the original array to produce a simpler array.
// Making an empty array
const arr = [];
//Generating the estimate IDs and placing them all in their own object in their own array.
function estimateGen(length, nodes) {
for (var i = 0; i < length; i++) {
const estimate = [];
let estimateVal = Math.random().toString(36).replace('0.','').slice(0,7);
estimate.push(estimateVal);
arr.push(estimate);
nodeGen(estimate, nodes)
}
}
// Adding x amount of nodes between 1 and 10 into each estimate sub array in their own objects.
function nodeGen(estimate, nodes) {
for (var i = 0; i < nodes; i++) {
let nodeID = Math.floor(Math.random() * 10) + 1;
estimate.push(nodeID);
}
}
// Calling the function and saying how many nodes per estimate we want.
estimateGen(5, 3);
console.log(arr);
From this code I now get the result:
[
[ 'p68xw8h', 5, 4, 6 ],
[ 'wn2yoee', 5, 4, 5 ],
[ '1w01tem', 9, 7, 4 ],
[ 'we3s53f', 8, 8, 8 ],
[ '5nrtp09', 3, 3, 8 ]
]
Would there be a way to count the number of times the values on the right appear and what 'estimate' ID at [0] it appears in?
Thank you.
First, let's redesign your input data and results to be a more useful format:
// input
[
{ nodes: [3, 4, 6], Estimate: '0jvyt8a' },
{ nodes: [4, 6, 3], Estimate: 'mc973fs' },
{ nodes: [4, 1, 3], Estimate: 'vwsfh8k' },
{ nodes: [4, 3, 5], Estimate: 'n6xzge3' },
{ nodes: [8, 7, 1], Estimate: 'v0jn7bh' }
];
// result
{
1: ["vwsfh8k", "v0jn7bh"],
3: ["0jvyt8a", "mc973fs", "vwsfh8k", "n6xzge3"],
...
]
Then the code would be:
const input = [
{ nodes: [3, 4, 6], Estimate: '0jvyt8a' },
{ nodes: [4, 6, 3], Estimate: 'mc973fs' },
{ nodes: [4, 1, 3], Estimate: 'vwsfh8k' },
{ nodes: [4, 3, 5], Estimate: 'n6xzge3' },
{ nodes: [8, 7, 1], Estimate: 'v0jn7bh' }
];
const result = {};
input.forEach(({
nodes,
Estimate: e
}) =>
nodes.forEach(n => {
if (!result[n]) {
result[n] = [];
}
result[n].push(e);
})
);
console.log(result);
You can create the data with:
// Making an empty array
const arr = [];
//Generating the estimate IDs and placing them all in their own object in their own array.
function estimateGen(length, nodes) {
for (var i = 0; i < length; i++) {
let estimateVal = Math.random().toString(36).replace('0.', '').slice(0, 7);
const estimate = {
Estimate: estimateVal,
nodes: []
}
arr.push(estimate);
nodeGen(estimate, nodes)
}
}
// Adding x amount of nodes between 1 and 10 into each estimate sub array in their own objects.
function nodeGen(estimate, nodes) {
for (var i = 0; i < nodes; i++) {
let nodeID = Math.floor(Math.random() * 10) + 1;
estimate.nodes.push(nodeID);
}
}
// Calling the function and saying how many nodes per estimate we want.
estimateGen(5, 3);
console.log(arr);
I've reformatted your array. The output is different, but you can still use it.
var arr = [
{ '0': 3 , '1': 4 , '2': 6 , 'Estimate:': '0jvyt8a' },
{ '0': 4 , '1': 6 , '2': 3 , 'Estimate:': 'mc973fs' },
{ '0': 4 , '1': 1 , '2': 3 , 'Estimate:': 'vwsfh8k' },
{ '0': 4 , '1': 3 , '2': 5 , 'Estimate:': 'n6xzge3' },
{ '0': 8 , '1': 7 , '2': 1 , 'Estimate:': 'v0jn7bh' }
];
var num = [1, 3, 4, 5, 6, 7, 8];
num = num.map(n =>
[n, ...(
arr.filter(a => [0, 1, 2].some(nm => a[nm] === n))
.map(v => v["Estimate:"])
)]);
console.log(num);
For getting a counting object you could take the values as key and estimates as key for the count of same values.
function estimateGen(length, nodes) {
var array = [];
for (var i = 0; i < length; i++) {
array.push([Math.random().toString(36).replace('0.','').slice(0,7), ...nodeGen(nodes)]);
}
return array;
}
function nodeGen(nodes) {
var result = [];
for (var i = 0; i < nodes; i++) {
result.push(Math.floor(Math.random() * 10) + 1);
}
return result;
}
function count(data) {
return data.reduce((r, [estimate, ...values]) => {
values.forEach(v => {
r[v] = r[v] || {};
r[v][estimate] = (r[v][estimate] || 0) + 1;
});
return r;
}, {});
}
var temp = estimateGen(5, 3);
console.log(temp);
console.log(count(temp));
.as-console-wrapper { max-height: 100% !important; top: 0; }

I want to set rank to array value?

I tried to create new array object from array , set rank according to its value . If value is the same, set the same rank and if next value is different set rank by skipping same rank length .
Expected result is
[
{
"rank": 1,
"data": 45
},
{
"rank": 2,
"data": 33
},
{
"rank": 3,
"data": 8
},
{
"rank": 4,
"data": 5
},
{
"rank": 4,
"data": 5
},
{
"rank": 6,
"data": 2
}
]
var data = [8,5,2,33,5,45];
var rankList = [];
var uniqueList = [];
var rank = 0;
var sameRank = 0;
data.sort(function(a,b) { return b - a; });
for(var i in data) {
if(uniqueList.includes(data[i])) {
rank++;
rankList.push({rank: sameRank, data: data[i]});
continue;
}
rank++;
sameRank++;
rankList.push({rank: rank, data: data[i]});
}
console.log(rankList);
Once you've sorted the array, create another array of objects with .map, keeping track of the last rank and data used. If the new data is the same, use the same rank (the one taken from a prior iteration) - otherwise, use the current iteration index plus 1:
const data = [8, 5, 2, 33, 5, 45];
data.sort((a, b) => b - a);
let lastRank = 0;
let lastData;
const output = data.map((data, i) => {
const objToReturn = { data };
objToReturn.rank = data === lastData ? lastRank : i + 1;
lastData = data;
lastRank = objToReturn.rank;
return objToReturn;
});
console.log(output);

Generate valid output from array values in Javascript

Maybe this isn't the right place to ask this but I need a advice since I'm stuck on this. I have this code:
$(document).ready(function(){
var i = 0, j = 0,
manufacturerIds = [1,2,3,4,5,6,7],
countryIds = [1,2,3,4,5,6,7,8,9,10],
result = [];
for (i; i < manufacturerIds.length; i++) {
for (j; j < countryIds.length; j++) {
result.push({
i: {
idMan: manufacturerIds[i],
idCtr: [] // stuck on this point, don't know
// where to go from here and don't know
// if I'm doing things right
}
});
}
}
});
And I'm trying to return a output like this:
[
{
"0": {
"idMan": 1,
"idCtr": [
1,
2,
3,
4,
5
]
},
"1": {
"idMan": 2,
"idCtr": [
1,
2,
3,
4,
5
]
},
"2": {
"idMan": 3,
"idCtr": [
1,
2,
3,
4,
5
]
}
}
]
Can any give me some advice? Or help?
NOTE: I'm not sure this is the right or best way but I'm trying to build some kind of structure where I can differentiate each item on the object|array, why? Because I'll need to add new element to it. For example, this ouput will be valid too:
[
{
"0": {
"idMan": 1,
"idCtr": [
1,
2
]
},
"1": {
"idMan": 1,
"idCtr": [
1,
4,
5
]
},
"2": {
"idMan": 1,
"idCtr": [
3
]
}
}
]
Then having this I think will be easy to add new idCtr right? By accesing someVar[X].idCtr.push(newVal);. BTW, I write some var examples but the truth is those values are dynamic, just a start point for you to get the idea behind my doubt
I believe this is more along the lines of what you are wanting
var i = 0,
j = 0,
manufacturerIds = [1, 2, 3, 4, 5, 6, 7],
countryIds = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
result = [];
for (i; i < manufacturerIds.length; i++) {
/* create basic object*/
var item = {};
item[i] = {idMan: manufacturerIds[i],idCtr: []};
/* now push country Ids*/
for (var j=0;; j < countryIds.length; j++) {
item[i].idCtr.push(countryIds[j]);
}
/* finished creating object*/
result.push(item);
}
DEMO
You can use JSON.stringify() to convert the result to JSON. There is another problem with i: {} try something like this:
$(document).ready(function(){
var i = 0, j = 0,
manufacturerIds = [1,2,3,4,5,6,7],
countryIds = [1,2,3,4,5,6,7,8,9,10],
result = [];
for (i; i < manufacturerIds.length; i++) {
var obj = {};
obj[i] = { idMan: manufacturerIds[i], idCtr: [] };
for (j; j < countryIds.length; j++) {
obj[i].idCtr.push(countryIds[j]);
}
result.push(obj);
}
var data = JSON.stringify(result);
});

Categories

Resources