J'ai objet de
{
0: "United States",
1: "India",
2: "Germany",
3: "Brazil",
4: "Taiwan",
5: "Israel",
6: "United Kingdom"
}
Je veux juste que ce soit comme utiliser jQuery
ou Javascript
seulement
["United States", "India", "Germany".....]
J'ai déjà essayé des codes comme
.reduce (), .flat () ou .concat ()
Mais ne fonctionne toujours pas
-6
Mark Salvania
20 juin 2019 à 22:08
4 réponses
Meilleure réponse
Solution 1
var obj= {
0: "United States",
1: "India",
2: "Germany",
3: "Brazil",
4: "Taiwan",
5: "Israel",
6: "United Kingdom"
}
var result = Object.values(obj)
console.log(result);
Solution 2
var result = Object.keys(obj).map(function(key) {
return obj[key];
});
console.log(result);
0
Nitin Daddikar
20 juin 2019 à 19:17
Vous pouvez affecter l'objet à un tableau. Cette approche respecte les clés.
var object = { 0: "United States", 1: "India", 2: "Germany", 3: "Brazil", 4: "Taiwan", 5: "Israel", 6: "United Kingdom" },
array = Object.assign([], object);
console.log(array);
1
Nina Scholz
20 juin 2019 à 19:15
const object = {
0: 'United States',
1: 'India',
2: 'Germany',
3: 'Brazil',
4: 'Taiwan',
5: 'Israel',
6: 'United Kingdom'
};
const countries = Object.values(object);
Les pays contiendront votre liste souhaitée
0
Melvin Vermeer
20 juin 2019 à 19:18
$.map({0:"united states", 1: "India"}, function(q,w){return(w)})
> ["united states", "India"]
1
ceo_9_palkad.com
20 juin 2019 à 19:40
Questions connexes
De nouvelles questions
javascript
Pour des questions concernant la programmation dans ECMAScript (JavaScript / JS) et ses divers dialectes / implémentations (hors ActionScript). Veuillez inclure toutes les balises pertinentes dans votre question; par exemple, [node.js], [jquery], [json], etc.