Reduce Array to objects that share certain property: values

Simon R.

> Everything is in here: https://jsfiddle.net/qr6hdbx0/

This is my code:

var viewport = {
    width: 'undefined',
    height: 'undefined',
    ratio: 1.7
};

var bg_1280x720 = {
    name: 'RZ_BG_1280x720_16-9.png',
    width: '1280',
    height: '720',
    ratio: 1.7
};

var bg_1920x1080 = {
    name: 'RZ_BG_1280x720_16-9.png',
    width: '1280',
    height: '720',
    ratio: 1.7
};

var bg_2000x2000 = {
    name: 'RZ_BG_2000x2000_1-1.png',
    width: '2000',
    height: '2000',
    ratio: 1
};

bgOverlays = [
bg_1280x720,
bg_1920x1080,
bg_2000x2000
]

I need a script, that looks at: viewport.ratio and then looks at the .ratio: values of all the objects in: bgOverlays and then creates a new array that has only the objects in it, that are closest to: viewport.ratio, in terms of their: .ratio: value.

The expected result in this example would be: newArray [bg_1280x720, bg_1920x1080]

I don't know how to do this, I found tutorials that show, how to reduce an array to only one value, that is closest to a given value, but in my case there might be two object in the array that have the same value, and I didn't get it to work… – I would very much appreciate any sort of input. Thank You! – Simon

The Bomb Squad

The logic is to make an array of the differences between the .ratio values of viewpoint from each object, thus finding the smallest number from that and matching that value with what makes that value in the array of objects.. hope I didn't confuse anyone

var viewport = {
    width: 'undefined',
    height: 'undefined',
    ratio: 1.6
};

var bg_1280x720 = {
    name: 'RZ_BG_1280x720_16-9.png',
    width: '1280',
    height: '720',
  ratio: 1.7
};

var bg_1920x1080 = {
    name: 'RZ_BG_1280x720_16-9.png',
    width: '1280',
    height: '720',
  ratio: 1.7
};

var bg_2000x2000 = {
    name: 'RZ_BG_2000x2000_1-1.png',
    width: '2000',
    height: '2000',
  ratio: 1
};

bgOverlays = [
bg_1280x720,
bg_1920x1080,
bg_2000x2000
]

// I need a script, that looks at: "viewport.ratio" and then looks at the ".ratio: values" of all the objects in: "bgOverlays" and then creates a new array that has only the objects in it, that are closest to: "viewport.ratio", in terms of their: ".ratio: value".

// example:
// viewport.ratio = 1.1     output: newArray[bg_2000x2000]
// viewport.ratio = 2           output: newArray[bg_1280x720, bg_1920x1080]
//well here you go :)

var low=bgOverlays
  .map(a=>Math.abs(a.ratio-viewport.ratio))
  .sort((a,b)=>a-b)[0]

var newArray=bgOverlays
  .filter(a=>Math.abs(a.ratio-viewport.ratio)==low)
console.log(newArray)

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

Inherited child objects share the same array property in JavaScript?

분류에서Dev

transform JSON array of objects, create new property from two property values

분류에서Dev

Reduce JSON object containing JSON objects into an array of the form [Path, Value]

분류에서Dev

filter an array of objects based on an object with an array as it's property value in react

분류에서Dev

lodash/underscore find objects by key that is in values of array

분류에서Dev

lodash/underscore find objects by key that is in values of array

분류에서Dev

How to get all values of objects inside array

분류에서Dev

MongoDB C# Query Array of Objects that contain a property value

분류에서Dev

angularjs filter array of objects based on specific attributes values

분류에서Dev

Search MongoDB Array of Objects where property has same value for more than 1 array element

분류에서Dev

C ++ std :: reduce with array

분류에서Dev

Finding the mean of certain values

분류에서Dev

Parse logs for certain values

분류에서Dev

Adding the values from an object to an array of objects without overriding existing key values

분류에서Dev

Angularjs filtering over array of objects in ngRepeat doesn't restore items with empty search property

분류에서Dev

How to refer to each property of an object in an array of objects in MongoDB MapReduce JavaScript query?

분류에서Dev

php array of objects can't get object property (Object of class stdClass could not be converted to string)

분류에서Dev

How to access a property in array of objects without looping through all of them in php?

분류에서Dev

Array into an array of objects in Java

분류에서Dev

PHP Accessing Values quickly an Arrray holding objects which one element of the object has an array

분류에서Dev

Getting all keys & their values from nested objects in JSON array (w/out jQuery)

분류에서Dev

How can I merge two complex JSON objects with only unique or different values only showing in resultant array

분류에서Dev

How do I sort an array filled with objects, based on two values of the object?

분류에서Dev

Remove string using certain values (-)

분류에서Dev

How to enlarge and reduce an already drawn lines, using certain buttons?

분류에서Dev

Assigning value to array at certain index

분류에서Dev

JS Array property extraction

분류에서Dev

Global Array property definition

분류에서Dev

Filter and uniq array of objects

Related 관련 기사

  1. 1

    Inherited child objects share the same array property in JavaScript?

  2. 2

    transform JSON array of objects, create new property from two property values

  3. 3

    Reduce JSON object containing JSON objects into an array of the form [Path, Value]

  4. 4

    filter an array of objects based on an object with an array as it's property value in react

  5. 5

    lodash/underscore find objects by key that is in values of array

  6. 6

    lodash/underscore find objects by key that is in values of array

  7. 7

    How to get all values of objects inside array

  8. 8

    MongoDB C# Query Array of Objects that contain a property value

  9. 9

    angularjs filter array of objects based on specific attributes values

  10. 10

    Search MongoDB Array of Objects where property has same value for more than 1 array element

  11. 11

    C ++ std :: reduce with array

  12. 12

    Finding the mean of certain values

  13. 13

    Parse logs for certain values

  14. 14

    Adding the values from an object to an array of objects without overriding existing key values

  15. 15

    Angularjs filtering over array of objects in ngRepeat doesn't restore items with empty search property

  16. 16

    How to refer to each property of an object in an array of objects in MongoDB MapReduce JavaScript query?

  17. 17

    php array of objects can't get object property (Object of class stdClass could not be converted to string)

  18. 18

    How to access a property in array of objects without looping through all of them in php?

  19. 19

    Array into an array of objects in Java

  20. 20

    PHP Accessing Values quickly an Arrray holding objects which one element of the object has an array

  21. 21

    Getting all keys & their values from nested objects in JSON array (w/out jQuery)

  22. 22

    How can I merge two complex JSON objects with only unique or different values only showing in resultant array

  23. 23

    How do I sort an array filled with objects, based on two values of the object?

  24. 24

    Remove string using certain values (-)

  25. 25

    How to enlarge and reduce an already drawn lines, using certain buttons?

  26. 26

    Assigning value to array at certain index

  27. 27

    JS Array property extraction

  28. 28

    Global Array property definition

  29. 29

    Filter and uniq array of objects

뜨겁다태그

보관