Understanding Arrays in Objects [closed] - javascript

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Rephrasing my question because I broke it down simpler than I actually did it :-P
I'm still trying to get the hang of relationships between objects and arrays.
I am trying to work on playing with an object full of arrays and I'd like to find the length of arrays in my object, but the way I'm going about it is not working.
var patterns = {
a: [2, 4, 12, 14],
b: [2, 4, 8, 12, 14],
c: [2, 4, 6, 8, 10, 12, 14],
d: [2, 4, 6, 7, 8, 9, 10, 12, 14],
e: [2, 3, 4, 6, 7, 8, 9, 10, 12, 13, 14],
f: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15],
};
for(var pattern in patterns){
console.log(patterns.pattern.length);
}
So what is a better...nay, the actual, way I'm should do this? And for the sake of learning, what is wrong with what I did?

How do you know patterns.c.length is wrong?
> var patterns = {
a: [2, 4, 12, 14],
b: [2, 4, 8, 12, 14],
c: [2, 4, 6, 8, 10, 12, 14],
d: [2, 4, 6, 7, 8, 9, 10, 12, 14],
e: [2, 3, 4, 6, 7, 8, 9, 10, 12, 13, 14],
f: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15],
};
> patterns.c
[2, 4, 6, 8, 10, 12, 14]
> patterns.c.length
7

Related

Divide matrix to sub matrices (Javascript) [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 1 year ago.
Improve this question
I need this for a codewars challenge to make my code DRY
I have an array of arrays, lets say a 9x9 matrix.
const sudokuTest2= [
[5, 3, 4, 6, 7, 8, 9, 1, 2],
[6, 7, 2, 1, 9, 0, 3, 4, 9],
[1, 0, 0, 3, 4, 2, 5, 6, 0],
[8, 5, 9, 7, 6, 1, 0, 2, 0],
[4, 2, 6, 8, 5, 3, 7, 9, 1],
[7, 1, 3, 9, 2, 4, 8, 5, 6],
[9, 0, 1, 5, 3, 7, 2, 1, 4],
[2, 8, 7, 4, 1, 9, 6, 3, 5],
[3, 0, 0, 4, 8, 1, 1, 7, 9],
];
How can I divide it automatically into 9 equal areas, something which results from the following sample code:
const areas = [];
areas[0] = sudokuTest2[0]
.slice(0, 3)
.concat(sudokuTest2[1].slice(0, 3))
.concat(sudokuTest2[2].slice(0, 3));
areas[1] = sudokuTest2[3]
.slice(0, 3)
.concat(sudokuTest2[4].slice(0, 3))
.concat(sudokuTest2[5].slice(0, 3));
areas[2] = sudokuTest2[6]
.slice(0, 3)
.concat(sudokuTest2[7].slice(0, 3))
.concat(sudokuTest2[8].slice(0, 3));
areas[3] = sudokuTest2[0]
.slice(3, 6)
.concat(sudokuTest2[1].slice(3, 6))
.concat(sudokuTest2[2].slice(3, 6));
areas[4] = sudokuTest2[3]
.slice(3, 6)
.concat(sudokuTest2[4].slice(3, 6))
.concat(sudokuTest2[5].slice(3, 6));
areas[5] = sudokuTest2[6]
.slice(3, 6)
.concat(sudokuTest2[7].slice(3, 6))
.concat(sudokuTest2[8].slice(3, 6));
areas[6] = sudokuTest2[0]
.slice(6, 9)
.concat(sudokuTest2[1].slice(6, 9))
.concat(sudokuTest2[2].slice(6, 9));
areas[7] = sudokuTest2[3]
.slice(6, 9)
.concat(sudokuTest2[4].slice(6, 9))
.concat(sudokuTest2[5].slice(6, 9));
areas[8] = sudokuTest2[6]
.slice(6, 9)
.concat(sudokuTest2[7].slice(6, 9))
.concat(sudokuTest2[8].slice(6, 9));
For sure I could use some nested loops, but I'm curious if there is any solution with array methods.
What will be the best solution, what do you think?
You could take generate the wanted 3x3 parts by using a nested mapping.
const
sudoku = [[5, 3, 4, 6, 7, 8, 9, 1, 2], [6, 7, 2, 1, 9, 0, 3, 4, 9], [1, 0, 0, 3, 4, 2, 5, 6, 0], [8, 5, 9, 7, 6, 1, 0, 2, 0], [4, 2, 6, 8, 5, 3, 7, 9, 1], [7, 1, 3, 9, 2, 4, 8, 5, 6], [9, 0, 1, 5, 3, 7, 2, 1, 4], [2, 8, 7, 4, 1, 9, 6, 3, 5], [3, 0, 0, 4, 8, 1, 1, 7, 9]],
areas = Array
.from({ length: 3 })
.flatMap((_, i) => Array
.from(
{ length: 3 },
(__, j) => Array
.from({ length: 3 })
.flatMap((___, k) => sudoku[j * 3 + k].slice(i * 3, (i + 1) * 3)
)
));
areas.forEach(a => console.log(...a));
This can be done in with single forEach method. The idea behind this is dynamic indexing.
const matrix = [ [5, 3, 4, 6, 7, 8, 9, 1, 2], [6, 7, 2, 1, 9, 0, 3, 4, 9], [1, 0, 0, 3, 4, 2, 5, 6, 0], [8, 5, 9, 7, 6, 1, 0, 2, 0], [4, 2, 6, 8, 5, 3, 7, 9, 1], [7, 1, 3, 9, 2, 4, 8, 5, 6], [9, 0, 1, 5, 3, 7, 2, 1, 4], [2, 8, 7, 4, 1, 9, 6, 3, 5], [3, 0, 0, 4, 8, 1, 1, 7, 9],];
let areas = new Array(9).fill([]);
matrix.forEach((numbers, idx)=> {
let segment = Math.floor(idx / 3);
areas[segment] = [...areas[segment], ...matrix[idx].slice(0, 3)];
areas[segment + 3] = [...areas[segment + 3], ...matrix[idx].slice(3, 6)];
areas[segment + 6] = [...areas[segment + 6], ...matrix[idx].slice(6, 9)];
})
areas.forEach(area => console.log(...area));

custom sort method in jquery/javascript

I want to sort a Array in custom way
Given arary:
arry = [0,0,1,1,2,2,3,3,4,4,5,5,6,6,7,7,8,8,9,9,10,10,11,11,12,12,13,13,14,14,15,15,16,16,17,17,18,18,21,21,22,22,23,23]
Expected output:
[21,22,22,23,23,0,0,1,1,2,2,3,3,4,4,5,5,6,6,7,7,8,8,9,9,10,10,11,11,12,12,13,13,14,14,15,15,16,16,17,17,18]
You could check the value first and the sort number greater than 20 first.
var array = [0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10, 10, 11, 11, 12, 12, 13, 13, 14, 14, 15, 15, 16, 16, 17, 17, 18, 18, 21, 21, 22, 22, 23, 23],
predicate = function (v) { return v > 20; };
array.sort(function(a, b) {
return predicate(b) - predicate(a) || a - b;
});
console.log(array);

Splice function to get rid of 6/7th of the entries in an array

I'm trying to understand where i'm going wrong here. say I have an array:
result = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]
I want to get rid of every 6 entries in my array, leave the 7th there, and go one. In effect i'm hoping to get rid of 6/7th of the values in my array. I'm thinking splice() could be perfect for that, but somehow the code below doesn't seem to work. Any suggestions?
Many thanks to you all!
function random_select ()
{
for(var i=0; i < result.length; i+7)
{
result.splice[i,6];
}
};
Try this instead:
var result = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16];
for (var i = 0; i < result.length; i++) result.splice(i, 6);
This will:
First remove [0, 1, 2, 3, 4, 5], and let [6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16] be.
Next remove [7, 8, 9, 10, 11, 12] and let [13, 14, 15, 16] be.
Finally remove [13, 14, 15, 16].
Hence the result is [6, 13]. Is this what you want?

How do I iterate through this object using JavaScript?

Let's say I have the follow:
var test_data = {
'numGroup1': [[(1, 2, 3, 4, 5), (5, 6, 7, 8, 9)]],
'numGroup2': [[(10, 11, 12, 13, 14), (15, 16, 17, 18, 19)]],
};
How would I go about iterating through it using JavaScript?
var test_data = {
'numGroup1': [[1, 2, 3, 4, 5], [5, 6, 7, 8, 9]],
'numGroup2': [[10, 11, 12, 13, 14], [15, 16, 17, 18, 19]],
};
for(var key in test_data){
group = test_data[key];
for(var num in group){
console.log(group[num]);
}
}
#Ian is right... using () will not do anything but enter the last digit of each group. You should use a multidimensional array
'numGroup1': [[1, 2, 3, 4, 5], [5, 6, 7, 8, 9]],
'numGroup2': [[10, 11, 12, 13, 14], [15, 16, 17, 18, 19]],
You can use underscorejs to iterate over it
var test_data = {
'numGroup1': [[1, 2, 3, 4, 5], [5, 6, 7, 8, 9]],
'numGroup2': [[10, 11, 12, 13, 14], [15, 16, 17, 18, 19]],
};
_.chain(test_data).map(function(value, key) {
return value;
}).flatten().each(alert);

Add specific values of multidimensional array together Javascript

Okay, so I have a multidimensional array that itself contains 9 arrays. Each of these nested arrays contains 10 numeric values. For sake of simplicity, let's say it all looks like this:
var MyArray = [
[1,2,3,4,5,6,7,8,9,10],
[1,2,3,4,5,6,7,8,9,10],
[1,2,3,4,5,6,7,8,9,10],
[1,2,3,4,5,6,7,8,9,10],
[1,2,3,4,5,6,7,8,9,10],
[1,2,3,4,5,6,7,8,9,10],
[1,2,3,4,5,6,7,8,9,10],
[1,2,3,4,5,6,7,8,9,10],
[1,2,3,4,5,6,7,8,9,10]
]
I am trying to write a function that will take the first index of each nested array (in this case, all 1's) and add them together, pushing this value either to an array or an object. Then, I need this function to continue on, adding all the values of the next index, and the next, and so on and so forth. In the end, I should have an array of 10 values (or an object works here as well). The values would be:
1+1+1+1+1+1+1+1+1,
2+2+2+2+2+2+2+2+2,
3+3+3+3+3+3+3+3+3...
...and so on so forth, so that the actual values of the new array would be this:
[9, 18, 27, 36, 45, 54, 63, 72, 81]
The catch here is that I need this to by flexible/dynamic, so that it will work in case MyArray has only 6 arrays, or maybe the nested arrays have only 4 values each. It should work with any amount of nested arrays, each with their own amount of values (though each nested array will contain the SAME amount of values as one another!).
What would be the best way to accomplish this via JavaScript and/or jQuery? Note that I could also have the values output to an object, in this fashion:
{1:9, 2:18, 3:27, 4:36, 5:45, 6:54, 7:63, 8:72, 9:81}
I tried using similar code to this from another StackOverflow thread to get an object, but it is returning
{1:NaN, 2:NaN, 3:NaN, etc.}
That thread can be found here:
Javascript Multidimensional Array: Add Values
I'm using the "underscore" method and the jQuery $.each part of it provided by Otto.
Anyone able to help here??
Something like this
var myData = [
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
];
var summed = [];
myData[0].forEach(function (arr, index) {
var sum = myData.reduce(function (a, b) {
return a + b[index];
}, 0);
summed.push(sum);
});
console.log(summed);
On jsfiddle
Here is another solution:
var MyArray = [
[1,2,3,4,5,6,7,8,9,10],
[1,2,3,4,5,6,7,8,9,10],
[1,2,3,4,5,6,7,8,9,10],
[1,2,3,4,5,6,7,8,9,10],
[1,2,3,4,5,6,7,8,9,10],
[1,2,3,4,5,6,7,8,9,10],
[1,2,3,4,5,6,7,8,9,10],
[1,2,3,4,5,6,7,8,9,10],
[1,2,3,4,5,6,7,8,9,10]
]
var results= [];
MyArray.map(function(a){
for(var i=0;i<a.length;i++){
if(results.length === a.length){
results[i] = results[i] + a[i];
}else{
results.push(a[i]);
}
}
});
http://jsfiddle.net/uMPAA/
A simple array solution would be the following :
var results= [];
for (var i=0;i<MyArray.length;i++) {
for(var j=0; j<MyArray[i].length; j++) {
if(results[j] == undefined) { results[j] = 0; }
results[j] = results[j]+data[i][j];
}
}
Note the if(results[j]==undefined) line -- this is probably what you didn't do. If you omit that, you get NaN on all lines, since you're adding an undefined value to a number.
Another approach to sum columns in multi-dimensional arrays (based on Lodash 4).
var arrays = [
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
];
function sum_col(arrays) {
return _.map(_.unzip(arrays), _.sum);
}
console.log(sum_col(arrays));
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.js"></script>

Categories

Resources