I'm getting array of data from server, but after come to jquery datatable i needs multidimention array .Is there any way to make it in jquery itself beflore pass it to datatables ?
My input format is :
["computer","program","laptop","monitor","mouse","keybord","cpu","harddrive"......]
Expected format :
[["computer","program","laptop","monitor"],["mouse","keybord","cpu","harddrive"],[....],[....]........]
Is there any method to parse data format ?
It doesn't take much more than a simple while loop to transform the array.
// This is the original data we get from the server
var input = ["computer","program","laptop","monitor","mouse","keybord","cpu","harddrive"];
// Make a copy of the input, so we don't destroy it
var data = input.slice(0);
// This is our output array
var output = [], group;
// A while loop will transform the plain array into a multidimensional array
while (data.length > 0) {
// Take the first four items
group = data.splice(0, 4);
// Make sure the group contains 4 items, otherwise pad with empty string
while (group.length < 4) {
group.push("");
}
// Push group into the output array
output.push(group);
}
// output = [["computer","program","laptop","monitor"],["mouse","keybord","cpu","harddrive"]]
Update: Beetroot-Beetroot's comment is no longer valid since we create a copy of the input.
I found this beautiful question sometime ago when i had a similar problem. This is a solution based (erm.. ripped out from there) on that :
var a = ["computer", "program", "laptop", "monitor", "mouse", "keybord", "cpu", "harddrive", "tablet"],
n = a.length / 4,
len = a.length,
out = [],
i = 0;
while (i < len) {
var size = Math.ceil((len - i) / n--);
out.push(a.slice(i, i + size));
i += size;
}
alert(JSON.stringify(out));
Message from the future ;) - now we have reduce:
function groupArray(array, groupSize) {
return array.reduce((a, b, i) => {
if (!i || !(i % groupSize)) a.push([])
a.slice(-1).pop().push(b)
return a
}, [])
}
console.log(groupArray(input, 4))
// [
// [ 'computer', 'program', 'laptop', 'monitor' ],
// [ 'mouse', 'keybord', 'cpu', 'harddrive' ]
// ]
Related
i've been working for some homework in replicating a json parse like function and currently i wrote a function as follows :
var arr = '[a, [a, bjng], n]'
var intoArray = function(string) {
if (string.length <= 2) {
return [];
}
var resultsArr = [];
var toJoin = [];
var simpArr = string.slice(1, string.length - 1).replace(/['"]+/g, '').replace(/[' '']+/g, '').split(',');
for (var a = 0; a < simpArr.length; a++) {
if (simpArr[a].includes('[')) {
while (!simpArr[a].includes(']')) {
toJoin.push(simpArr[a]);
a++;
}
}
if (simpArr[a].includes(']')) {
toJoin.push(simpArr[a]);
}
}
var joined = toJoin.join(',')
var joint = joined.slice(1, joined.length - 1).replace(/['"]+/g, '').replace(/[' '']+/g, '').split(',')
for (var a = 0; a < simpArr.length; a++) {
if (!toJoin.includes(simpArr[a])) {
resultsArr.push(simpArr[a])
}
if (simpArr[a] === toJoin[0]) {
resultsArr[a] = joint
}
}
return resultsArr
};
console.log(intoArray(arr))
when run it converts this string '[a, [a, bjng], n]'(this is all just one big string) into [ 'a', [ 'a', 'bjng' ], 'n' ] (this is an array with multiple string and another array inside, this is the desired behavior). However when the sample string has more than one array inside of it like '[a, [a, bjng],[c] , n]' the result is that it is taken as just one bigger array like [ 'a', [ 'a', 'bjng]', '[c' ], 'n' ] (notice how instead of creating two array it thinks that '[a' trough 'c]' is one big array), i'm still learning so some insight would be much appreciated or if someone has some idea of how i can adapt it to handle multiple arrays!!! or perhaps a better implemntation?
Logic is identical to that of my last answer to your similar question here, except it uses chunks of consecutive characters instead of individual characters in order to handle strings longer than 1.
const string = '[a, [a, bjng], n]'.slice(1, -1);
const chunks = string.match(/\w+|[\[\]|,| ]/g);
const array = [];
let currArr = array;
for (const chunk of chunks) {
/\w/.test(chunk) && currArr.push(chunk);
chunk == ']' && (currArr = array);
if (chunk == '[') {
currArr = [];
array.push(currArr);
}
}
console.log(array); // [ 'a', [ 'a', 'bjng' ], 'n' ]
#GirkovArpa made a good start, but as #Barmar already mentioned: you will need some kind of recursion if you want to process arbitrary (deeper) array structures correctly. Here is my (rudimentary) take on it:
const string = '[a, [ [d,e,[f,g]], bjng], [c], n]';
function parse(str){
function add2arr(a,p){ var c
do c=p.shift()
while( c && (/\w/.test(c)&&a.push(c)
|| c==','
|| c=='[' &&a.push(add2arr([],p))) );
return a
}
const parts = str.replace(/\s*/g,"").match(/\w+|[\[\],]/g);
if(parts.shift()=='[') return add2arr([],parts);
}
console.log(parse(string))
// or, much simpler: use JSON.parse:
console.log(JSON.parse(string.replace(/(\w+)/g,'"$1"')));
I have the following array of objects:
var transactions = [
[
{"id":1,"sourceAccount":"A","targetAccount":"B","amount":100,"category":"food","time":"2018-03-02T10:33:00.000Z"},
{"id":2,"sourceAccount":"A","targetAccount":"B","amount":100,"category":"food","time":"2018-03-02T10:33:50.000Z"},
{"id":3,"sourceAccount":"A","targetAccount":"B","amount":100,"category":"food","time":"2018-03-02T10:34:30.000Z"},
{"id":4,"sourceAccount":"A","targetAccount":"B","amount":100,"category":"food","time":"2018-03-02T10:36:00.000Z"}
],
[
{"id":5,"sourceAccount":"A","targetAccount":"C","amount":250,"category":"other","time":"2018-03-02T10:33:00.000Z"},
{"id":6,"sourceAccount":"A","targetAccount":"C","amount":250,"category":"other","time":"2018-03-02T10:33:05.000Z"}
]
]
I need to compare time property of each object consecutively, and keep only those properties which time difference between each consecutive transaction is less than 1 minute.
The array format should be stay same, this is what I did try, but no luck, didn't work. What's the problem?
var newArray = transactions.map(g => g.reduce((r, o, i, a) => {
if (!i || new Date(o.time).getTime() - new Date(a[i - 1].time).getTime() >= 60000) {
r.push([o]);
} else {
r[r.length - 1].push(o);
}
return r;
}, []));
The expected output is something like this :
var output = [
[
{"id":1,"sourceAccount":"A","targetAccount":"B","amount":100,"category":"food","time":"2018-03-02T10:33:00.000Z"},
{"id":2,"sourceAccount":"A","targetAccount":"B","amount":100,"category":"food","time":"2018-03-02T10:33:50.000Z"},
{"id":3,"sourceAccount":"A","targetAccount":"B","amount":100,"category":"food","time":"2018-03-02T10:34:30.000Z"}
],
[
{"id":5,"sourceAccount":"A","targetAccount":"C","amount":250,"category":"other","time":"2018-03-02T10:33:00.000Z"},
{"id":6,"sourceAccount":"A","targetAccount":"C","amount":250,"category":"other","time":"2018-03-02T10:33:05.000Z"}
]
]
You can Array#map your source array, and in each iteration, Array#filter the desired elements by comparing the time of current element with the time of previous element.
var transactions = [[{"id":1,"sourceAccount":"A","targetAccount":"B","amount":100,"category":"food","time":"2018-03-02T10:33:00.000Z"},{"id":2,"sourceAccount":"A","targetAccount":"B","amount":100,"category":"food","time":"2018-03-02T10:33:50.000Z"},{"id":3,"sourceAccount":"A","targetAccount":"B","amount":100,"category":"food","time":"2018-03-02T10:34:30.000Z"},{"id":4,"sourceAccount":"A","targetAccount":"B","amount":100,"category":"food","time":"2018-03-02T10:36:00.000Z"}],[{"id":5,"sourceAccount":"A","targetAccount":"C","amount":250,"category":"other","time":"2018-03-02T10:33:00.000Z"},{"id":6,"sourceAccount":"A","targetAccount":"C","amount":250,"category":"other","time":"2018-03-02T10:33:05.000Z"}]];
var result = transactions.map((tr, i) => {
return tr.filter((t, j) => {
if (transactions[i][j - 1]) {
var d1 = new Date(t.time);
var d2 = new Date(transactions[i][j - 1].time);
return (d1.getTime() - d2.getTime()) <= 60000;
}
return true;
});
});
console.log(result);
I get an Array with an unknown Number of data.
But I only have an predefined amount of data to be shown/store.
How can I take every nth Element of the initial Array and reduce it in JavaScript?
Eg.: I get an Array with size=10000, but are only able to show n=2k Elements.
I tried it like that:
delta= Math.round(10*n/size)/10 = 0.2 -> take every 5th Element of the initial Array.
for (i = 0; i < oldArr.length; i++) {
arr[i] = oldArr[i].filter(function (value, index, ar) {
if (index % delta != 0) return false;
return true;
});
}
With 0.2 it´s always 0, but with some other deltas (0.3) it is working. Same for delta=0.4, i works, but every second Element is taken with that. What can I do to get this to work?
Maybe one solution :
avoid filter because you don't want to loop over 10 000 elements !
just access them directly with a for loop !
var log = function(val){document.body.innerHTML+='<div></pre>'+val+'</pre></div>'}
var oldArr = [0,1,2,3,4,5,6,7,8,9,10]
var arr = [];
var maxVal = 5;
var delta = Math.floor( oldArr.length / maxVal );
// avoid filter because you don't want
// to loop over 10000 elements !
// just access them directly with a for loop !
// |
// V
for (i = 0; i < oldArr.length; i=i+delta) {
arr.push(oldArr[i]);
}
log('delta : ' + delta + ' length = ' + oldArr.length) ;
log(arr);
Filter itself returns an array. If I'm understanding you correctly, you don't need that surrounding loop. So:
newArr = oldArr.filter(function(value, index, Arr) {
return index % 3 == 0;
});
will set newArr to every third value in oldArr.
Try
arr = oldArr.filter(function (value, index, ar) {
return (index % ratio == 0);
} );
where ratio is 2 if you want arr to be 1/2 of oldArr, 3 if you want it to be 1/3 of oldArr and so on.
ratio = Math.ceil(oldArr.length / size); // size in the new `arr` size
You were calling filter() on each element of oldAdd inside a loop and you're supposed to call filter() on the whole array to get a new filtered array back.
Borrowing from #anonomyous0day's solution, generate a new Array with the desired indices from the given array:
(Take every 3 items)
Array.prototype.take = function(n) {
if (!Number(n) && n !== 0) {
throw new TypeError(`Array.take requires passing in a number. Passed in ${typeof n}`);
} else if (n <= 0) {
throw new RangeError(`Array.take requires a number greater than 0. Passed in ${n}`);
}
const selectedIndicesLength = Math.floor(this.length / n);
return [...Array(selectedIndicesLength)].map((item, index) => this[index * n + 1]);
};
[1, 2, 3, 4, 5, 6, 7, 8].take(2); // => 2, 4, 6, 8
this also works by using map to create the new array without iterating over all elements in the old array..
// create array with 10k entries
const oldArr = [ ...Array( 10000 ) ].map( ( _el, i ) => i );
const max = 10;
const delta = Math.floor( oldArr.length / max );
const newArr = [ ...Array( max ) ].map( ( _el, i ) => (
oldArr[ i * delta ]
) );
console.log( newArr );
may help!
const myFunction = (a, n) => {
let array = []
for(i = n; i <= a.length; i += n){
array.push(a[i-1]);
}
return array;
}
I have an array of team names which is like this:
teams = ['team1', 'team2', 'team3', 'team4']
I want to create a set of matches based on this, so at the moment, for a set of matches using the teams array you would get:
teamMatches : [
[ teams[0], teams[1] ],
[ teams[2], teams[3] ]
]
My question is, can anyone think of a way to produce this teamMatches array based upon a teams array with length n. The teams array will always be a correct number, 4,8,16 etc. I have tried using Math.pow with 2 to try to produce the brackets with no success.
Any ideas?
A procedural solution:
xs = [0,1,2,3,4,5,6,7,8,9]
result = []
n = 2 // n=3 gives a list of triples, etc
for(var i = 0; i < xs.length; i += n)
result.push(xs.slice(i, i + n))
A functional programming solution:
function zip() {
var args = [].slice.call(arguments, 0);
return args[0].map(function(_, n) {
return args.map(function(a) {return a[n] })
})
}
xs = [0,1,2,3,4,5,6,7,8,9]
result = zip(
xs.filter(function(_, n) { return n % 2 == 0 }),
xs.filter(function(_, n) { return n % 2 != 0 })
)
// [[0,1],[2,3],[4,5],[6,7],[8,9]]
Explanation: zip is a function (built-in in some programming languages, but not in Javascript) that takes N arrays and groups together elements at the same position:
zip([1,2,3], [10,20,30]) -> [ [1,10], [2,20], [3,30] ]
We split an array into even and odd parts:
[0,2,4,6...]
[1,3,5,7...]
and zip them together, getting a list of ordered pairs.
How about this
var teams = ['team1', 'team2', 'team3', 'team4'];
var teamMatches = new Array();
for(var i=0;i<teams.length; i+=2)
{
teamMatches.push([teams[i],teams[i+1]]);
}
I have an A array with n length.
I want to take all possible k (0
for example, if i have A's length is five:
[1,2,3,4,5]
and if k = 3, algorithm must give me B array.
[1,2,3 ]
[1,2, 4 ]
[1,2, 5]
[1, 3,4 ]
[1, 3, 5]
[1, 4,5]
[ 2,3,4 ]
[ 2,3, 5]
[ 2, 4,5]
[ 3,4,5]
Length of B would be equal to n!/k!(n-k)! ('!' means factorial, Newtons method)
I'm using javascript, so in my tags i included it, but it's just algorithm, not necessary written in javascript.
You could do this via a filter method.
In your example you want to receive all permutations of an array, taking a specific number of elements of that array.
You can easily do that in an iterative manner.
Start by taking all permutations of n - 1 elements of an array:
// return all (n - 1) element permutations of an array
var permutations = function(arr) {
return arr.reduce(function(re, value, i) {
// add an array for each element in the original array
return re.concat([arr.filter(function(v, index) {
// drop each element with the same index
return index !== i
})])
}, [])
}
Now permutations([1,2,3]) would return [[1,2], [1,3], [2,3]]
That's always a disjoint set suppose you're having only unique values in the source array.
To receive all 3-element arrays of a 5-element array, you would first calculate the list of 4-element arrays and transform each of them to a 3-element array.
permutations([1,2,3,4]).map(permutations)
=> [[1,2,3] => [[[1,2], [1,3], [2,3]]
,[1,2,4] ,[[1,2], [1,4], [2,4]]
,[1,3,4] ,[[1,3], [1,4], [3,4]]
,[2,3,4] ,[[2,3], [2,4], [3,4]]
] ]
Obviously the problem here is that there are doubles.
That can be solved by dropping all non-unique values.
var unique = function(arr) {
var s = arr.map(function(v) { return "" + v })
return arr.filter(function(v, i) { return s.indexOf("" + v) == i })
}
Packing it all into one function could be done like this:
var permutationsWithLength = function(arr, length) {
var re = [arr]
for (var i = arr.length; i >= length; i--) {
re = re.reduce(function(tmp, perms) {
return unique(temp.concat(permutations(perms)))
}, [])
}
return re
}
I admit that this may not be the fastest approach, especially regarding the unique function, but it's a very generic one and will work for the problem you described even with larger arrays.
Hope it helps ;)
Below is the copy-paste from one of my projects. Don't know if it still works ;)
var choose = function choose_func(elems, len) {
var result = [];
for (var i=0; i<elems.length; i++) {
if (len == 1) {
result.push([elems[i]]);
} else {
var remainingItems = choose_func(elems.slice(i+1, elems.length), len - 1);
for (var j=0; j<remainingItems.length; j++)
result.push([elems[i]].concat(remainingItems[j]));
}
}
return result;
};
var result = choose([1,2,3,4,5], 3)
/*result = [[1,2,3],[1,2,4],[1,2,5],[1,3,4],[1,3,5],
[1,4,5],[2,3,4],[2,3,5],[2,4,5],[3,4,5]] */