0

Is there a way to slice with javascript an array but not according to the index of the dimension but according to the content of its' j-th element (supposedly uniqueness of values)?

Example:

myArray = [["a", "b", "c"], ["car", "hat", "plate"], ["red", "green", "black"]] 

Slicing myArray on 0,1 will give

[["c"],["plate"],["black"]]

and slicing on "black" should give the same result.

What is the use of my question? Say for example that my array comes from parsing the rows of a file and I want to slice upon the headers (first row), but the headers of the -to be parsed- files (although same) come in random orders.

Thanks

3
  • you can get the index using Array.indexOf('plate') Commented Aug 14, 2015 at 2:17
  • 1
    What are the meaning of the parameters 0, 1? And why do these parameters produce the same value as calling with 'black' as a parameter? Commented Aug 14, 2015 at 2:18
  • 1
    Also, don't redefine Array; it's a constructor for arrays and redefining it will break a lot of things Commented Aug 14, 2015 at 2:22

2 Answers 2

2

You can simply loop through the 2D array and call slice(j) on each array:

for(item in myArray)
    Array[item] = myArray[item].slice(2);

Here is an example snippet:

var myArray = [["a", "b", "c"], ["car", "hat", "plate"], ["red", "green", "black"]];

for(item in myArray)
    myArray[item] = myArray[item].slice(2);

console.log(myArray);

Using "black" as a paramter involes going through each array and finding such an element. When you find a match simply call indexOf() to get the proper index and use that, which will be 2:

var index = "black";

for(item in myArray)
    if(myArray[item].indexOf(index) != -1)
        index = myArray[item].indexOf(index);

// index = 2

Note: There will be problems when there is more than a single "black", in this case index will be the last "black" found in myArray.

Sign up to request clarification or add additional context in comments.

8 Comments

This code happens to produce the right answer for the example, but it doesn't really make sense with some of the details of the question: namely, the parameters to the slice function (0, 1 and 'black').
@NoahFreitas I think the OP means by 0,1 as to remove items in the 0,1 index and keep the ones at 2. and 'black' simply being the 2nd element of one of the arrays.
@NoahFreitas : You are correct Noah, I want to use "black" as a parameter and through time it may be on different indexes which I do not know.
@pebox11, and you want all the other arrays filtered down to only the value at the same index as 'black'?
@pebox11, this code does not currently do that, since it has the index (2) hard-coded. @codeSun's answer does suggest how to do this.
|
1

If you want to use value of array element for search, use indexOf().

 myArray = [["a", "b", "c"], ["car", "hat", "plate"], ["red", "green", "black"]];

    var index = myArray.indexOf("black");

    for(value in myArray)
        myArray[value] = myArray[value].slice(index);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.