0

I ran into a little bit of a problem with manipulation of data, since I don't know JavaScript too well. I have the following array:

var Dict = [["x", "y", "z"], ["x", "w", "m"], ["u", "t", "d", "L"]];

What I need is to select all first elements, then all the second elements etc. I've tried

for(var m in Dict) {
    First.push(m[0]);
    Second.push(m[1]);
    Third.push(m[2]);
}

But that doesn't seem to work. Then I also need to delete for example all "x" from the top array, which I haven't even attempted yet, because of the first problem.

2
  • any errors in your console? Commented Feb 19, 2016 at 19:15
  • It is an array. Use for (var i=0;i<dict.length;i++) or dict.forEach or a map Commented Feb 19, 2016 at 19:15

6 Answers 6

1
var item, First=[], Second=[], Third=[], Dict = [["x", "y", "z"],["x", "w", "m"], ["u", "t", "d", "L"]];
for(var m in Dict) {
    // m does not refer to item in Dict, but index of item in Dict
    First.push(Dict[m][0]);
    Second.push(Dict[m][1]);
    Third.push(Dict[m][2]);
}
console.log(First, Second, Third);
// ["x", "x", "u"] ["y", "w", "t"] ["z", "m", "d"]
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, I wasnt sure in the indexes, and couldnt find a guide for arrays in arrays. Works like a charm
1

If you have access to es6, you can do this:

const Dict = [["x", "y", "z"],["x", "w", "m"], ["u", "t", "d", "L"]];
const [First, Second, Third] = Dict.map((_, i) => Dict.map(entry => return entry[i]));

The es5 version would be:

var Dict = [["x", "y", "z"],["x", "w", "m"], ["u", "t", "d", "L"]];
var Items = Dict.map(function(_, i) {
  return Dict.map(function(entry) {
    return entry[i];
  });
});

var First  = Items.shift(), 
    Second = Items.shift(), 
    Third  = Items.shift();

Comments

0

Try the following javascript, this should work :

var Dict = [
  ["x", "y", "z"],
  ["x", "w", "m"],
  ["u", "t", "d", "L"]
];
First = [];
Second = [];
Third = [];

for (m=0;m<Dict.length;m++) {
  First.push(Dict[m][0]);
  Second.push(Dict[m][1]);
  Third.push(Dict[m][2]);
}
console.log(First);
console.log(Second);
console.log(Third);

Gives the following three arrays :

["x", "x", "u"]
["y", "w", "t"]
["z", "m", "d"]

Comments

0

Do not use for-in with arrays. To get all first elements:

ES5

var firstElements = Dict.map(function(el) { return el[0]; }); 

ES6

var firstElements = Dict.map(el => el[0]); 

(yields ["x", "x", "u"])

Comments

0

Try this:

var Dict = [["x", "y", "z"],["x", "w", "m"], ["u", "t", "d", "L"]];
var first = Dict.slice(0,1);
var second = Dict.slice(1,2);
var third = Dict.slice(-1);

To see what that produces go here:

JSFiddle

A Better Alternative

If you want to do it with a variable set of indices you can use splice instead of slice:

var Dict = [["x", "y", "z"],["x", "w", "m"], ["u", "t", "d", "L"]];
var result = [];
for(var i = 0; i < Dict.length; i++){
   result.push(Dict.splice(i));
}

You can see this here:

JSFiddle Splice

Comments

0

Check this piece of code

var Dict = [["x", "y", "z"],["x", "w", "m"], ["u", "t", "d", "L"]];
for(var i=0; i < Dict.length;i++){
  allArr[i] = Dict.map(function(subArr){
    return subArr[i]
  });
  
  //To Remove all the "x" from the array
  if(i == 0){
    allArr[i] = allArr[i].filter(val => val != "x")
  }
}

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.