how to get all the combination from a group of arrays - javascript

I have for example these arrays:
a1 = ["1", "2", "3"];
a2 = ["a", "b"];
a3 = ["q", "w", "e"];
result = ["1aq", "1aw", "1ae", "1bq", "1bw", ... "3be"];
How could obtain this without nested loops (also using jquery, for example)?
Thanks

I don't see what's wrong with nested loops, but here is a generic solution:
var a = [a1, a2, a3];
var result = [""]; // start with the empty string,
for (var i=0; i<a.length; i++) { // and repeatedly
var ai = a[i],
l = ai.length;
result = $.map(result, function(r) { // make result a new array of
var ns = []; // new combinations of
for (var j=0; j<l; j++) // each of the letters in ai
ns[j] = r + ai[j]; // and the old results
return ns;
}); // using the odds of jQuery.map with returned arrays
}
return result;

No nested loops. Can handle as many arrays as needed.
var result = combine(a1, a2, a3);
function combine() {
return processArrays([].slice.call(arguments), "", []);
function processArrays(arrays, str, res) {
for (var i = 0; i < arrays[0].length; i++) {
if (arrays.length > 1) {
processArrays(arrays.slice(1), str + arrays[0][i], res);
} else {
res.push(str + arrays[0][i]);
}
}
return res;
}
}
Or a slightly different take on the function:
function combine() {
return processArrays([].slice.call(arguments), "", []);
function processArrays(arrays, str, res) {
if (arrays.length === 0)
res.push(str)
else
for (var i = 0; i < arrays[0].length; i++)
processArrays(arrays.slice(1), str + arrays[0][i], res);
return res;
}
}
And here's a no loops version:
var result = combine(a1, a2, a3);
function combine() {
return processArrays(arguments[0], [].slice.call(arguments, 1), "", []);
function processArrays(head, tail, str, res) {
if (head === undefined)
res.push(str)
else
processArray(head[0], head.slice(1), tail, str, res);
return res;
}
function processArray(head, tail, arrays, str, res) {
if (head) {
processArrays(arrays[0], arrays.slice(1), str + head, res);
processArray(tail[0], tail.slice(1), arrays, str, res)
}
}
}

A generic recursive solution:
function combine() {
var target = arguments[0];
if (arguments.length === 1) {
return target; // end of chain, just return the array
}
var result = [];
// compute all combinations without the first array
var combinations = combine.apply(null, Array.prototype.slice.call(arguments, 1));
// put things together
for (var i = 0, l = target.length; i < l; i++) {
var element = target[i];
for (var j = 0, lj = combinations.length; j < lj; j++) {
result.push(element + combinations[j]);
}
}
return result;
}
// Usage
var result = combine(a1, a2, a3);

Another generic solution.
var reduce = function(a, b) {
var r = [];
$.each(a, function(i, ai) {
$.each(b, function(j, bj) {
r.push(ai + bj);
});
});
return r;
};
var result = reduce(reduce(a1, a2), a3);

var outputArray = [];
for(var i = 0, finalLength = a1.length * a2.length * a3.length; i < finalLength; i++) {
outputArray[i] = a1[i % a1.length].toString() + a2[i % a2.length].toString() + a3[i % a3.length].toString();
}
But this is really just a stunt. Why avoid the loops? I can guess: You don't know in advance how many arrays you'll have. But it's still going to be a challenge.

Related

Javascript - Permutations remove the duplicates elements

that is found here in stack but i want somes changes.
function perms(data) {
if (!(data instanceof Array)) {
throw new TypeError("input data must be an Array");
}
data = data.slice(); // make a copy
var permutations = [],
stack = [];
function doPerm() {
if (data.length == 0) {
permutations.push(stack.slice());
}
for (var i = 0; i < data.length; i++) {
var x = data.splice(i,1);
stack.push(x);
doPerm();
stack.pop();
data.splice(i, 0, x);
}
}
doPerm();
return permutations;
}
var input = "552".split('');
var result = perms(input);
for (var i = 0; i < result.length; i++) {
result[i] = result[i].join('-');
}
The result of that is :
5-5-2
5-2-5
5-5-2
5-2-5
2-5-5
2-5-5
but , are 3 elements duplicates the result must be :
5-5-2
5-2-5
2-5-5
how can i fix that issue .
Basically, you have one issue,
var x = data.splice(i, 1)[0];
// ^^^ is missing
because you get an array with splicing. The result is a deep nested array with
data.splice(i, 0, x);
This inserts the array later on position i.
For preventing duplicates, you need a check, if the actual value is already inserted in the result set with
permutations.some(function (a) {
return a.every(function (b, j) {
return stack[j] === b;
});
}) || permutations.push(stack.slice());
which test the arrays and if no match, the push is performed.
function perms(data) {
if (!(data instanceof Array)) {
throw new TypeError("input data must be an Array");
}
data = data.slice(); // make a copy
var permutations = [],
stack = [],
hash = Object.create(null);
function doPerm() {
if (data.length == 0) {
permutations.some(function (a) {
return a.every(function (b, j) {
return stack[j] === b;
});
}) || permutations.push(stack.slice());
return;
}
for (var i = 0; i < data.length; i++) {
var x = data.splice(i, 1)[0];
stack.push(x);
doPerm();
stack.pop();
data.splice(i, 0, x);
}
}
doPerm();
return permutations;
}
var input = "552".split('');
var result = perms(input);
for (var i = 0; i < result.length; i++) {
result[i] = result[i].join('-');
}
console.log(result);
Check if array array is present within resulting array before calling .push() using Array.prototype.some(), Array.prototype.join()
function p(a, b, res) {
var b = b || [],
res = res || [],
len = a.length;
if (!len) {
// check if `res` contains `b.join("")`
if (!res.length
|| !res.some(function(n) {
return n.join("") === b.join("")
}))
res.push(b)
} else {
for (var i = 0
; i < len; p(a.slice(0, i).concat(a.slice(i + 1, len))
, b.concat(a[i]), res)
, i++);
}
return res
}
var result = p("552".split(""));
result = result.map(function(res) {
return res.join("-")
});
console.log(result);

How to get all possible combinations of elements in an array including order and lengths

function getAllCombinations(arr) {
var f = function(arr) {
var result = [];
var temp = [];
for (var i = 0; i < arr.length; i++) {
temp = [];
temp.push(arr[i]);
result.push(temp);
for (var j = 0; j < arr.length; j++) {
if (j != i) {
temp = [];
temp.push(arr[i]);
temp.push(arr[j]);
result.push(temp);
for (var k = 0; k < arr.length; k++) {
if (k != i && k != j) {
temp = [];
temp.push(arr[i]);
temp.push(arr[j]);
temp.push(arr[k]);
result.push(temp);
for (var l = 0; l < arr.length; l++) {
if (l != i && l != j && l != k) {
temp = [];
temp.push(arr[i]);
temp.push(arr[j]);
temp.push(arr[k]);
temp.push(arr[l]);
result.push(temp);
}
}
}
}
}
}
}
return result;
}
return f(arr);
}
//call this function
console.log(getAllCombinations(["a", "b", "c", "d"]));
[["a"],["a","b"],["a","b","c"],["a","b","c","d"],["a","b","d"],["a","b","d","c"],["a","c"],["a","c","b"],["a","c","b","d"],["a","c","d"],["a","c","d","b"],["a","d"],["a","d","b"],["a","d","b","c"],["a","d","c"],["a","d","c","b"],["b"],["b","a"],["b","a","c"],["b","a","c","d"],["b","a","d"],["b","a","d","c"],["b","c"],["b","c","a"],["b","c","a","d"],["b","c","d"],["b","c","d","a"],["b","d"],["b","d","a"],["b","d","a","c"],["b","d","c"],["b","d","c","a"],["c"],["c","a"],["c","a","b"],["c","a","b","d"],["c","a","d"],["c","a","d","b"],["c","b"],["c","b","a"],["c","b","a","d"],["c","b","d"],["c","b","d","a"],["c","d"],["c","d","a"],["c","d","a","b"],["c","d","b"],["c","d","b","a"],["d"],["d","a"],["d","a","b"],["d","a","b","c"],["d","a","c"],["d","a","c","b"],["d","b"],["d","b","a"],["d","b","a","c"],["d","b","c"],["d","b","c","a"],["d","c"],["d","c","a"],["d","c","a","b"],["d","c","b"],["d","c","b","a"]]
A total of 64 combinations for a 4 length array.
The function works fine but I need to make this function recursive. The for loops have to be nested based on the length of the array and the push also increased per nested loop.
Really appreciate some advice.
Finally made it recursive !!
Tried to work down on the the original code posted above moving each loop functionality into simple functions.
function getAllCombinations(inputArray) {
var resultArray = [];
var combine = function() {
for (var i in inputArray) {
var temp = [];
var tempResult = [];
for (var j in arguments) {
tempResult.push(inputArray[arguments[j]]);
if (arguments[j] == i) {
temp = false;
} else if (temp) {
temp.push(arguments[j]);
}
}
if (temp) {
temp.push(i);
combine.apply(null, temp);
}
}
if (tempResult.length > 0) {
resultArray.push(tempResult);
}
return resultArray;
};
return combine();
}
See the older version here.
Result produces 64 unique combinations for a 4 dimensional array
console.log(getAllCombinations(["a", "b", "c", "d"]));
[["a","b","c","d"],["a","b","c"],["a","b","d","c"],["a","b","d"],["a","b"],["a","c","b","d"],["a","c","b"],["a","c","d","b"],["a","c","d"],["a","c"],["a","d","b","c"],["a","d","b"],["a","d","c","b"],["a","d","c"],["a","d"],["a"],["b","a","c","d"],["b","a","c"],["b","a","d","c"],["b","a","d"],["b","a"],["b","c","a","d"],["b","c","a"],["b","c","d","a"],["b","c","d"],["b","c"],["b","d","a","c"],["b","d","a"],["b","d","c","a"],["b","d","c"],["b","d"],["b"],["c","a","b","d"],["c","a","b"],["c","a","d","b"],["c","a","d"],["c","a"],["c","b","a","d"],["c","b","a"],["c","b","d","a"],["c","b","d"],["c","b"],["c","d","a","b"],["c","d","a"],["c","d","b","a"],["c","d","b"],["c","d"],["c"],["d","a","b","c"],["d","a","b"],["d","a","c","b"],["d","a","c"],["d","a"],["d","b","a","c"],["d","b","a"],["d","b","c","a"],["d","b","c"],["d","b"],["d","c","a","b"],["d","c","a"],["d","c","b","a"],["d","c","b"],["d","c"],["d"]]
Here is my solution using a subroutine, and closures. Also slice is very useful here.
If you found this helpful, or if you think other people will find this helpful, don't be afraid to upvote.
function getMyCombinations(coll) {
const result = [];
(function search(currentPerm, letters) {
if (letters.length === 0) return result.push(currentPerm);
let trimArray = letters.slice(1);
letters.forEach(letter => search(currentPerm + letter, trimArray));
})('', coll)
return result;
}
console.log(getMyCombinations(["a", "b", "c", "d"]));
I have refactored my original answer to better align with the users request.
function findPerm(array, currentPerm = '', result =[]) {
if (array.length === 0) return result;
let trimArray = array.slice(1);
array.forEach(v => {
let copy = [...result];
let perm = (currentPerm + v).split('');
let res = copy.push(perm);
result = findPerm(trimArray, currentPerm + v, copy);
});
return result;
};
console.log(findPerm(['a', 'b', 'c', 'd']));
An alternative solution, seems getting the desired output :)
console.log(JSON.stringify(getMyCombinations(["a", "b", "c", "d"])))
function getMyCombinations(arry) {
var len = arry.length;
var tempArray = [];
var result = []
var tempCount = 1;
var createCombinations = function(count){
var singleLevelArray = [];
arry.forEach(function(item){
if(count){//1
if(count > 1){
for(var j = 0; j < tempArray.length; j++){
if(tempArray[j].indexOf(item) === -1){
var x = tempArray[j].slice();
x.push(item);
singleLevelArray.push(x);
result.push(x);
}
}
} else {
for(var k = 0; k < len; k++){
if(item.indexOf(arry[k]) === -1){
tempArray.push([item, arry[k]]);
result.push([item, arry[k]]);
}
}
}
} else {
result.push([item]);
}
})
if(singleLevelArray.length){
tempArray = []
tempArray = singleLevelArray;
}
if(tempCount < len){
createCombinations(tempCount++);
}
return result;
}
return createCombinations()
}

Check for repeated characters in a string Javascript

I was wondering if there is a way to check for repeated characters in a string without using double loop. Can this be done with recursion?
An example of the code using double loop (return true or false based on if there are repeated characters in a string):
var charRepeats = function(str) {
for(var i = 0; i <= str.length; i++) {
for(var j = i+1; j <= str.length; j++) {
if(str[j] == str[i]) {
return false;
}
}
}
return true;
}
Many thanks in advance!
This will do:
function hasRepeats (str) {
return /(.).*\1/.test(str);
}
(A recursive solution can be found at the end of this answer)
You could simply use the builtin javascript Array functions some MDN some reference
var text = "test".split("");
text.some(function(v,i,a){
return a.lastIndexOf(v)!=i;
});
callback parameters:
v ... current value of the iteration
i ... current index of the iteration
a ... array being iterated
.split("") create an array from a string
.some(function(v,i,a){ ... }) goes through an array until the function returns true, and ends than right away. (it doesn't loop through the whole array, which is good for performance)
Details to the some function here in the documentation
Here some tests, with several different strings:
var texts = ["test", "rest", "why", "puss"];
for(var idx in texts){
var text = texts[idx].split("");
document.write(text + " -> " + text.some(function(v,i,a){return a.lastIndexOf(v)!=i;}) +"<br/>");
}
//tested on win7 in chrome 46+
If you will want recursion.
Update for recursion:
//recursive function
function checkString(text,index){
if((text.length - index)==0 ){ //stop condition
return false;
}else{
return checkString(text,index + 1)
|| text.substr(0, index).indexOf(text[index])!=-1;
}
}
// example Data to test
var texts = ["test", "rest", "why", "puss"];
for(var idx in texts){
var txt = texts[idx];
document.write( txt + " ->" + checkString(txt,0) + "<br/>");
}
//tested on win7 in chrome 46+
you can use .indexOf() and .lastIndexOf() to determine if an index is repeated. Meaning, if the first occurrence of the character is also the last occurrence, then you know it doesn't repeat. If not true, then it does repeat.
var example = 'hello';
var charRepeats = function(str) {
for (var i=0; i<str.length; i++) {
if ( str.indexOf(str[i]) !== str.lastIndexOf(str[i]) ) {
return false; // repeats
}
}
return true;
}
console.log( charRepeats(example) ); // 'false', because when it hits 'l', the indexOf and lastIndexOf are not the same.
function chkRepeat(word) {
var wordLower = word.toLowerCase();
var wordSet = new Set(wordLower);
var lenWord = wordLower.length;
var lenWordSet =wordSet.size;
if (lenWord === lenWordSet) {
return "false"
} else {
return'true'
}
}
Using regex to solve=>
function isIsogram(str){
return !/(\w).*\1/i.test(str);
}
console.log(isIsogram("isogram"), true );
console.log(isIsogram("aba"), false, "same chars may not be adjacent" );
console.log(isIsogram("moOse"), false, "same chars may not be same case" );
console.log(isIsogram("isIsogram"), false );
console.log(isIsogram(""), true, "an empty string is a valid isogram" );
The algorithm presented has a complexity of (1 + n - (1)) + (1 + n - (2)) + (1 + n - (3)) + ... + (1 + n - (n-1)) = (n-1)*(1 + n) - (n)(n-1)/2 = (n^2 + n - 2)/2 which is O(n2).
So it would be better to use an object to map and remember the characters to check for uniqueness or duplicates. Assuming a maximum data size for each character, this process will be an O(n) algorithm.
function charUnique(s) {
var r = {}, i, x;
for (i=0; i<s.length; i++) {
x = s[i];
if (r[x])
return false;
r[x] = true;
}
return true;
}
On a tiny test case, the function indeed runs a few times faster.
Note that JavaScript strings are defined as sequences of 16-bit unsigned integer values. http://bclary.com/2004/11/07/#a-4.3.16
Hence, we can still implement the same basic algorithm but using a much quicker array lookup rather than an object lookup. The result is approximately 100 times faster now.
var charRepeats = function(str) {
for (var i = 0; i <= str.length; i++) {
for (var j = i + 1; j <= str.length; j++) {
if (str[j] == str[i]) {
return false;
}
}
}
return true;
}
function charUnique(s) {
var r = {},
i, x;
for (i = 0; i < s.length; i++) {
x = s[i];
if (r[x])
return false;
r[x] = true;
}
return true;
}
function charUnique2(s) {
var r = {},
i, x;
for (i = s.length - 1; i > -1; i--) {
x = s[i];
if (r[x])
return false;
r[x] = true;
}
return true;
}
function charCodeUnique(s) {
var r = [],
i, x;
for (i = s.length - 1; i > -1; i--) {
x = s.charCodeAt(i);
if (r[x])
return false;
r[x] = true;
}
return true;
}
function regExpWay(s) {
return /(.).*\1/.test(s);
}
function timer(f) {
var i;
var t0;
var string = [];
for (i = 32; i < 127; i++)
string[string.length] = String.fromCharCode(i);
string = string.join('');
t0 = new Date();
for (i = 0; i < 10000; i++)
f(string);
return (new Date()) - t0;
}
document.write('O(n^2) = ',
timer(charRepeats), ';<br>O(n) = ',
timer(charUnique), ';<br>optimized O(n) = ',
timer(charUnique2), ';<br>more optimized O(n) = ',
timer(charCodeUnique), ';<br>regular expression way = ',
timer(regExpWay));
let myString = "Haammmzzzaaa";
myString = myString
.split("")
.filter((item, index, array) => array.indexOf(item) === index)
.join("");
console.log(myString); // "Hamza"
Another way of doing it using lodash
var _ = require("lodash");
var inputString = "HelLoo world!"
var checkRepeatition = function(inputString) {
let unique = _.uniq(inputString).join('');
if(inputString.length !== unique.length) {
return true; //duplicate characters present!
}
return false;
};
console.log(checkRepeatition(inputString.toLowerCase()));
const str = "afewreociwddwjej";
const repeatedChar=(str)=>{
const result = [];
const strArr = str.toLowerCase().split("").sort().join("").match(/(.)\1+/g);
if (strArr != null) {
strArr.forEach((elem) => {
result.push(elem[0]);
});
}
return result;
}
console.log(...repeatedChar(str));
You can also use the following code to find the repeated character in a string
//Finds character which are repeating in a string
var sample = "success";
function repeatFinder(str) {
let repeat="";
for (let i = 0; i < str.length; i++) {
for (let j = i + 1; j < str.length; j++) {
if (str.charAt(i) == str.charAt(j) && repeat.indexOf(str.charAt(j)) == -1) {
repeat += str.charAt(i);
}
}
}
return repeat;
}
console.log(repeatFinder(sample)); //output: sc
const checkRepeats = (str: string) => {
const arr = str.split('')
const obj: any = {}
for (let i = 0; i < arr.length; i++) {
if (obj[arr[i]]) {
return true
}
obj[arr[i]] = true
}
return false
}
console.log(checkRepeats('abcdea'))
function repeat(str){
let h =new Set()
for(let i=0;i<str.length-1;i++){
let a=str[i]
if(h.has(a)){
console.log(a)
}else{
h.add(a)
}
}
return 0
}
let str = '
function repeat(str){
let h =new Set()
for(let i=0;i<str.length-1;i++){
let a=str[i]
if(h.has(a)){
console.log(a)
}else{
h.add(a)
}
}
return 0
}
let str = 'haiiiiiiiiii'
console.log(repeat(str))
'
console.log(repeat(str))
Cleanest way for me:
Convert the string to an array
Make a set from the array
Compare the length of the set and the array
Example function:
function checkDuplicates(str) {
const strArray = str.split('');
if (strArray.length !== new Set(strArray).size) {
return true;
}
return false;
}
You can use "Set object"!
The Set object lets you store unique values of any type, whether
primitive values or object references. It has some methods to add or to check if a property exist in the object.
Read more about Sets at MDN
Here how i use it:
function isIsogram(str){
let obj = new Set();
for(let i = 0; i < str.length; i++){
if(obj.has(str[i])){
return false
}else{
obj.add(str[i])
}
}
return true
}
isIsogram("Dermatoglyphics") // true
isIsogram("aba")// false

How to merge elements in array by first words in JS? Is it possible?

I have the following array
['.some_class &.green_mod','.some_class &.red_mod','another_class &.green_mod','another_class &.orange_mod']
I want to get this array from it:
['.some_class &.green_mod &.red_mod','another_class &.green_mod &.orange_mod']
Is it possible?
you can try:
var preArr = ['.some_class &.green_mod', '.some_class &.red_mod', 'another_class &.green_mod', 'another_class &.orange_mod'];
var newArr = [];
preArr.forEach(function (item) {
var has = false;
var preWords = item.split('&');
for (var i = 0; i < newArr.length; ++i) {
var newWords = newArr[i].split('&');
if (newWords[0] == preWords[0]) {
has = true;
for (var j = 0; j < preWords.length; ++j) {
if (newWords.indexOf(preWords[j]) < 0) {
newWords.push(preWords[j]);
}
}
newArr[i] = newWords.join('&');
}
}
if (!has) {
newArr.push(item);
}
});
console.log(newArr);
demo
var test = (function() {
var fmap1 = function(e) { return e.trim().split(/\s+/); };
var fmap2 = function(e) { return e.join(" "); };
var fsort = function(e1,e2) { return e1[0] == e2[0] ? 0 : e1[0] > e2[0] ? 1 : -1; };
return function test(a) {
var a1 = a.map(fmap1).sort(fsort);
var s, a2 = [];
for (var i = 0, l = a1.length; i < l; i++) {
if (s != a1[i][0]) {
s = a1[i][0];
a2.push([s]);
}
a1.push.apply(a2[a2.length - 1], a1[i].slice(1));
}
return a2.map(fmap2);
};
})();
var arr = [' .some_class &.green_mod',' another_class &.green_mod','.some_class &.red_mod','another_class &.orange_mod'];
console.log(test(arr));
This seems to work for me:
var arr = ['.some_class &.green_mod','.some_class &.red_mod','another_class &.green_mod','another_class &.orange_mod'];
var obj = {};
var finalArr = [];
for(var i=0,c=arr.length;i<c;i++)
{
var parts = arr[i].split(' ');
var key = parts[0];
if(!obj[key]) obj[key] = [];
obj[key].push(parts.slice(1).join(' '));
}
var keys = Object.keys(obj);
for(var i=0,c=keys.length;i<c;i++)
{
var key = keys[i];
finalArr.push(key+' '+obj[key].join(' '))
}
console.log(finalArr);
Basically just loop through each one, and use the first word as a key to an array of strings to be appended, then loop through the object and join the keys with all of their array elements.
N.B. References to key are the first word
Solution bellow for data like key+separator+value where you are sure that there is no duplication in keys or You do not care if values are duplicated
// maping function that treats part before separator as key and saves incremetaly
// all values under that key (duplicated vales are possible)
// only one separator per input entry is allowed
function mapFun (el, obj, separator) {
var e = el.split(separator);
var key = e[0];
var val = e[1];
obj[key] = obj[key] ? obj[key] + separator + val : separator + val;
}
function combineClasses (arr) {
var result = [];
var separator = ' &';
var obj = Object.create(null); // create empty object without any properties or inheritance chain
for (var i=0; i<arr.length; i++) {
mapFun(arr[i], obj, separator);
}
for (var p in obj) {
result.push (p + obj[p]);
}
return result;
}
combineClasses(arr);
Bellow solution will work for data like before and data like key+separator+value1+separator+value2 and will not allow for duplicated values for the same key
function mapFun (el, separator, obj) {
var parts = el.split(separator);
var key = parts[0];
if (!obj[key]) {
obj[key] = Object.create(null);
}
for (var i=1; i<parts.length; i++) {
obj[key][parts[i]] = null;
}
}
function combineClasses (arr) {
var result = [];
var separator = ' &';
var obj = Object.create(null); // create empty object without any properties
for (var i=0; i<arr.length; i++) {
mapFun(arr[i], separator, obj);
}
for (var p in obj) {
var values = Object.keys(obj[p]).join(separator);
result.push (p + separator + values);
}
return result;
}
combineClasses(arr);
arr is where Your data comes in
var arr = ['.some_class &.green_mod','.some_class &.red_mod','another_class &.green_mod','another_class &.orange_mod']

Knowing position of a value under twodimensional Array

I have a SummaryData array as shown
var summaryData = [[0,100.34],[1,102.31],[2,131.08],[3,147.94],[4,172.55],[5,181.05],[6,180.08]];
My question is:
Is it possible to find out what the position of a value is?
(For example, how can I know where 147.94 is?) (I am expecting "3")
Update:
A more prototype-y way:
var result = summaryData.detect(function(item) { return item[1] === 147.94; });
alert(result[0]);
Or:
function getKey(arr, value) {
var key = null,
item;
for (var i = 0; i < arr.length && !key; i++) {
item = arr[i];
if (item[1] === value) {
key = i;
}
}
return key;
}
Usage:
var n = getKey(summaryData, 147.94); // returns 3.
At the risk of doing your homework for you...
var summaryData = [[0,100.34],[1,102.31],[2,131.08],[3,147.94],[4,172.55],[5,181.05],[6,180.08]];
function findPosition(value, dataArray) {
var a;
for (var i=0, iLen=dataArray.length; i<iLen; i++) {
a = dataArray[i];
for (var j=0, jLen=a.length; j<jLen; j++){
if (value == a[j]) {
return i + ',' + j;
}
}
}
}
alert(findPosition(131.08, summaryData)); // 2,1
The above returns the position of the first match.
Edit
I see now that you don't need to iterate over the second array, just look at the second value, so:
function findPosition(value, dataArray) {
var a;
for (var i=0, iLen=dataArray.length; i<iLen; i++) {
a = dataArray[i];
if (value == a[1]) {
return a[0];
}
}
}
alert(findPosition(131.08, summaryData)); //2
Or if the data format is always as specified and there may be thousands of values, then it may be much faster to do:
function findPosition(value, dataArray) {
var re = new RegExp('[^,],' + value);
var m = dataArray.join().match(re);
return m && m[0].replace(/,.*/,'');
// Or
// return m && m[0].split(',')[0];
}
function getPosition(candidate) {
var i = summaryData.length;
while (i) {
i -= 1;
if (summaryData[i][1] === candidate) {
return summaryData[i][0];
}
}
}

Categories

Resources