Map keys into array according to values
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
what's the most efficient way to map keys into array according to values based on the condition of the value?
For example, I have a map that contains the File object as the key and boolean as the value like this:
map = new Map([
[file, true],
[file1, true],
[file2, false],
[file3, true],
]);
Can I ask what's the shortcut way of creating an array of file objects if map value === true?
Intended outcome
files = [file,file1,file3];
Appreciate your help thanks!
javascript typescript dictionary angular6
add a comment |
what's the most efficient way to map keys into array according to values based on the condition of the value?
For example, I have a map that contains the File object as the key and boolean as the value like this:
map = new Map([
[file, true],
[file1, true],
[file2, false],
[file3, true],
]);
Can I ask what's the shortcut way of creating an array of file objects if map value === true?
Intended outcome
files = [file,file1,file3];
Appreciate your help thanks!
javascript typescript dictionary angular6
add a comment |
what's the most efficient way to map keys into array according to values based on the condition of the value?
For example, I have a map that contains the File object as the key and boolean as the value like this:
map = new Map([
[file, true],
[file1, true],
[file2, false],
[file3, true],
]);
Can I ask what's the shortcut way of creating an array of file objects if map value === true?
Intended outcome
files = [file,file1,file3];
Appreciate your help thanks!
javascript typescript dictionary angular6
what's the most efficient way to map keys into array according to values based on the condition of the value?
For example, I have a map that contains the File object as the key and boolean as the value like this:
map = new Map([
[file, true],
[file1, true],
[file2, false],
[file3, true],
]);
Can I ask what's the shortcut way of creating an array of file objects if map value === true?
Intended outcome
files = [file,file1,file3];
Appreciate your help thanks!
javascript typescript dictionary angular6
javascript typescript dictionary angular6
asked Jan 3 at 2:58
iBlehhziBlehhz
1229
1229
add a comment |
add a comment |
4 Answers
4
active
oldest
votes
You can use entries
to get the keys and the values of the map, then filter
it by whether a value (the condition) is true, and then .map
to turn the [key, value]
into just the key:
const map = new Map([
['file', true],
['file1', true],
['file2', false],
['file3', true],
]);
const files = [...map.entries()]
.filter(([_, cond]) => cond)
.map(([key]) => key);
console.log(files);
add a comment |
A simple forEach loop could do the trick here.
const map = new Map([
['test', true],
['test2', true],
['test3', false],
['test4', true],
]);
const files = ;
map.forEach((value, key, map) => {
if (value) {
files.push(key)
}
});
console.log(files);
add a comment |
You could use Map.entries
with filter
and map
to achieve this. The basic idea is
- Get an array of all keys/values with
Map.entries
- filter that array to only the entries with the value
true
- use map to convert from Map entries to array elements.
the code would look something like this.
map.entries().filter(entry=>entry[1]).map(entry=>entry[0]);
add a comment |
The answers above work but if you're after the fastest, perhaps surprisingly using a for loop is faster than the prototype Map and array methods
aka:
myMap = new Map([
["file", true],
["file1", true],
["file2", false],
["file3", true],
]);
const fastest = (files) => {
const map = [...files]
const out =
for (let i=0; i<map.length; i++){
if (map[i][1]){
out.push(map[i][0])
}
}
return out
}
console.log(fastest(myMap))
https://coderwall.com/p/kvzbpa/don-t-use-array-foreach-use-for-instead
There are many articles and a lot of literature about this if you have a look around
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54015767%2fmap-keys-into-array-according-to-values%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
You can use entries
to get the keys and the values of the map, then filter
it by whether a value (the condition) is true, and then .map
to turn the [key, value]
into just the key:
const map = new Map([
['file', true],
['file1', true],
['file2', false],
['file3', true],
]);
const files = [...map.entries()]
.filter(([_, cond]) => cond)
.map(([key]) => key);
console.log(files);
add a comment |
You can use entries
to get the keys and the values of the map, then filter
it by whether a value (the condition) is true, and then .map
to turn the [key, value]
into just the key:
const map = new Map([
['file', true],
['file1', true],
['file2', false],
['file3', true],
]);
const files = [...map.entries()]
.filter(([_, cond]) => cond)
.map(([key]) => key);
console.log(files);
add a comment |
You can use entries
to get the keys and the values of the map, then filter
it by whether a value (the condition) is true, and then .map
to turn the [key, value]
into just the key:
const map = new Map([
['file', true],
['file1', true],
['file2', false],
['file3', true],
]);
const files = [...map.entries()]
.filter(([_, cond]) => cond)
.map(([key]) => key);
console.log(files);
You can use entries
to get the keys and the values of the map, then filter
it by whether a value (the condition) is true, and then .map
to turn the [key, value]
into just the key:
const map = new Map([
['file', true],
['file1', true],
['file2', false],
['file3', true],
]);
const files = [...map.entries()]
.filter(([_, cond]) => cond)
.map(([key]) => key);
console.log(files);
const map = new Map([
['file', true],
['file1', true],
['file2', false],
['file3', true],
]);
const files = [...map.entries()]
.filter(([_, cond]) => cond)
.map(([key]) => key);
console.log(files);
const map = new Map([
['file', true],
['file1', true],
['file2', false],
['file3', true],
]);
const files = [...map.entries()]
.filter(([_, cond]) => cond)
.map(([key]) => key);
console.log(files);
answered Jan 3 at 3:05
CertainPerformanceCertainPerformance
97.5k165887
97.5k165887
add a comment |
add a comment |
A simple forEach loop could do the trick here.
const map = new Map([
['test', true],
['test2', true],
['test3', false],
['test4', true],
]);
const files = ;
map.forEach((value, key, map) => {
if (value) {
files.push(key)
}
});
console.log(files);
add a comment |
A simple forEach loop could do the trick here.
const map = new Map([
['test', true],
['test2', true],
['test3', false],
['test4', true],
]);
const files = ;
map.forEach((value, key, map) => {
if (value) {
files.push(key)
}
});
console.log(files);
add a comment |
A simple forEach loop could do the trick here.
const map = new Map([
['test', true],
['test2', true],
['test3', false],
['test4', true],
]);
const files = ;
map.forEach((value, key, map) => {
if (value) {
files.push(key)
}
});
console.log(files);
A simple forEach loop could do the trick here.
const map = new Map([
['test', true],
['test2', true],
['test3', false],
['test4', true],
]);
const files = ;
map.forEach((value, key, map) => {
if (value) {
files.push(key)
}
});
console.log(files);
const map = new Map([
['test', true],
['test2', true],
['test3', false],
['test4', true],
]);
const files = ;
map.forEach((value, key, map) => {
if (value) {
files.push(key)
}
});
console.log(files);
const map = new Map([
['test', true],
['test2', true],
['test3', false],
['test4', true],
]);
const files = ;
map.forEach((value, key, map) => {
if (value) {
files.push(key)
}
});
console.log(files);
answered Jan 3 at 3:06
XzandroXzandro
661410
661410
add a comment |
add a comment |
You could use Map.entries
with filter
and map
to achieve this. The basic idea is
- Get an array of all keys/values with
Map.entries
- filter that array to only the entries with the value
true
- use map to convert from Map entries to array elements.
the code would look something like this.
map.entries().filter(entry=>entry[1]).map(entry=>entry[0]);
add a comment |
You could use Map.entries
with filter
and map
to achieve this. The basic idea is
- Get an array of all keys/values with
Map.entries
- filter that array to only the entries with the value
true
- use map to convert from Map entries to array elements.
the code would look something like this.
map.entries().filter(entry=>entry[1]).map(entry=>entry[0]);
add a comment |
You could use Map.entries
with filter
and map
to achieve this. The basic idea is
- Get an array of all keys/values with
Map.entries
- filter that array to only the entries with the value
true
- use map to convert from Map entries to array elements.
the code would look something like this.
map.entries().filter(entry=>entry[1]).map(entry=>entry[0]);
You could use Map.entries
with filter
and map
to achieve this. The basic idea is
- Get an array of all keys/values with
Map.entries
- filter that array to only the entries with the value
true
- use map to convert from Map entries to array elements.
the code would look something like this.
map.entries().filter(entry=>entry[1]).map(entry=>entry[0]);
answered Jan 3 at 3:08
schu34schu34
644619
644619
add a comment |
add a comment |
The answers above work but if you're after the fastest, perhaps surprisingly using a for loop is faster than the prototype Map and array methods
aka:
myMap = new Map([
["file", true],
["file1", true],
["file2", false],
["file3", true],
]);
const fastest = (files) => {
const map = [...files]
const out =
for (let i=0; i<map.length; i++){
if (map[i][1]){
out.push(map[i][0])
}
}
return out
}
console.log(fastest(myMap))
https://coderwall.com/p/kvzbpa/don-t-use-array-foreach-use-for-instead
There are many articles and a lot of literature about this if you have a look around
add a comment |
The answers above work but if you're after the fastest, perhaps surprisingly using a for loop is faster than the prototype Map and array methods
aka:
myMap = new Map([
["file", true],
["file1", true],
["file2", false],
["file3", true],
]);
const fastest = (files) => {
const map = [...files]
const out =
for (let i=0; i<map.length; i++){
if (map[i][1]){
out.push(map[i][0])
}
}
return out
}
console.log(fastest(myMap))
https://coderwall.com/p/kvzbpa/don-t-use-array-foreach-use-for-instead
There are many articles and a lot of literature about this if you have a look around
add a comment |
The answers above work but if you're after the fastest, perhaps surprisingly using a for loop is faster than the prototype Map and array methods
aka:
myMap = new Map([
["file", true],
["file1", true],
["file2", false],
["file3", true],
]);
const fastest = (files) => {
const map = [...files]
const out =
for (let i=0; i<map.length; i++){
if (map[i][1]){
out.push(map[i][0])
}
}
return out
}
console.log(fastest(myMap))
https://coderwall.com/p/kvzbpa/don-t-use-array-foreach-use-for-instead
There are many articles and a lot of literature about this if you have a look around
The answers above work but if you're after the fastest, perhaps surprisingly using a for loop is faster than the prototype Map and array methods
aka:
myMap = new Map([
["file", true],
["file1", true],
["file2", false],
["file3", true],
]);
const fastest = (files) => {
const map = [...files]
const out =
for (let i=0; i<map.length; i++){
if (map[i][1]){
out.push(map[i][0])
}
}
return out
}
console.log(fastest(myMap))
https://coderwall.com/p/kvzbpa/don-t-use-array-foreach-use-for-instead
There are many articles and a lot of literature about this if you have a look around
answered Jan 3 at 3:25
Happy MachineHappy Machine
452213
452213
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54015767%2fmap-keys-into-array-according-to-values%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown