У меня есть объект следующим образом:
asset: {
images []
}
Изображения выглядят так:
image {
width,
height,
fileName
}
Мне нужно проецировать все имена файлов изображений в массив. Очевидная реализация заключается в следующем:
var fileNames = [];
var i;
for (i = 0; i < asset.images.length; i++) {
fileNames.push(asset.images[i].fileName);
}
Кто-нибудь знает, какой способ сделать это в 1 или 2 строках? Вы можете использовать jQuery или angularJs
var fileNames = asset.images.map(function(i) {
return i.fileName;
});
Это самый быстрый способ запустить набор инструкций по каждому элементу массива.
asset.images.map(function(i) {return i.fileName;};);
var asset = {};
asset.images = [];
asset.images.push({fileName: "test1"});
asset.images.push({fileName: "test2"});
asset.images.push({fileName: "test3"});
asset.images.push({fileName: "test4"});
alert(asset.images.map(function(i) {return i.fileName;}));
Ignored call to 'alert()'. The document is sandboxed, and the 'allow-modals' keyword is not set.