0

I have the next key:value array -

[["key1",76],["key2",73],["key3",59],["key4",52],["key5",37],["key6",7],["key7",5],["key8",5],["key9",3],["key10",2],["key11",2]]

And I would like to make an array out of it but only with the values of it and also to keep the order of the values, meaning the new array should be like this -

[76,73,59,52,37,7,5,5,3,2,2]

I've tried to find a way to do that but failed miserably,

Thanks in advanced for any kind of help

0

4 Answers 4

5

You can use map:

var arr1 = [["key1",76],["key2",73],["key3",59],["key4",52],["key5",37],["key6",7],["key7",5],["key8",5],["key9",3],["key10",2],["key11",2]];
var arr2 = arr1.map(function(v){ return v[1] });
Sign up to request clarification or add additional context in comments.

Comments

1

var arr = [["key1",76],["key2",73],["key3",59],["key4",52],["key5",37],["key6",7],["key7",5],["key8",5],["key9",3],["key10",2],["key11",2]];

var result = [];

arr.forEach(function(val,index){
    result.push(val[1]);
});

alert(JSON.stringify(result));

Comments

0

No need of looping through the array, you can use regex in your case to extract all the values from the nested array.

var target = [["key1",76],["key2",73],["key3",59],["key4",52],["key5",37],["key6",7],["key7",5],["key8",5],["key9",3],["key10",2],["key11",2]];

var result = target.toString().match(/\b\d+\b/g);

console.log(result);
document.write(result);

3 Comments

@DenysSéguret result is array. You can run the code and check console for output
Sorry, you're right, misread. It's still horrible to convert to a string
@DenysSéguret Just wanted to add it as another way to achieve the result. I could have use loops, but that would be duplicate of other answers
0
var res_list = [];
var ori_list = [["key1",76],["key2",73],["key3",59],["key4",52],["key5",37],["key6",7],["key7",5],["key8",5],["key9",3],["key10",2],["key11",2]];

for( var i=0, len=ori_list.length; i<len; i ++ ) {
    res_list.push( ori_list[i][1] );
}

1 Comment

There's no append function on arrays

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.