обеспечение REST API, доступного из Android

Хотелось бы объяснить пошаговый принятый ответ шаг за шагом:

var objects = [{ x: 3 }, { x: 1 }, { x: 2 }];

// array.map lets you extract an array of attribute values
var xValues = objects.map(function(o) { return o.x; });
// es6
xValues = Array.from(objects, o => o.x);

// function.apply lets you expand an array argument as individual arguments
// So the following is equivalent to Math.max(3, 1, 2)
// The first argument is "this" but since Math.max doesn't need it, null is fine
var xMax = Math.max.apply(null, xValues);
// es6
xMax = Math.max(...xValues);

// Finally, to find the object that has the maximum x value (note that result is array):
var maxXObjects = objects.filter(function(o) { return o.x === xMax; });

// Altogether
xMax = Math.max.apply(null, objects.map(function(o) { return o.x; }));
var maxXObject = objects.filter(function(o) { return o.x === xMax; })[0];
// es6
xMax = Math.max(...Array.from(objects, o => o.x));
maxXObject = objects.find(o => o.x === xMax);


document.write('

objects: ' + JSON.stringify(objects) + '

'); document.write('

xValues: ' + JSON.stringify(xValues) + '

'); document.write('

xMax: ' + JSON.stringify(xMax) + '

'); document.write('

maxXObjects: ' + JSON.stringify(maxXObjects) + '

'); document.write('

maxXObject: ' + JSON.stringify(maxXObject) + '

');

Дополнительная информация:

30
задан Shafiul 3 October 2011 в 04:26
поделиться