如何根据变量的值从数组中提取变量?

时间:2019-02-13 02:57:19

标签: javascript arrays variables

我将变量存储在数组中。现在,我需要提取具有最高值的变量并将它们存储在另一个数组中。

这是为我女儿在wix网站上运行的javascript程序。

var kiara = 1;
var rodeo = 3;
var calypso = 3;
var balthazar = 3;
var mistral = 4;
var saya = 4;
var luna = 4;

var points = [{kiara}, {rodeo}, {calypso}, {balthazar}, {mistral}, {saya}, {luna}],

变量已经按升序排列。因此,我需要程序找出在顶部位置出现的mistral,saya和luna,然后提取这3个变量(变量名称+相应值)并将它们存储在另一个数组中。如果我遇到单个变量具有最高值的情况,则单个变量将存储在新数组中。

4 个答案:

答案 0 :(得分:1)

由于数组已排序,因此您已经知道maximun值位于数组的最后一个位置。因此,首先,您可以获得该值,然后可以将该值用于filter元素,这些元素的值等于该值:

var kiara = 1;
var rodeo = 3;
var calypso = 3;
var balthazar = 3;
var mistral = 4;
var saya = 4;
var luna = 4;

var points = [{kiara}, {rodeo}, {calypso}, {balthazar}, {mistral}, {saya}, {luna}];

// Since array is ordered, highest value is at the last position.
// So, we can get it like this:
let maxVal = Object.values(points[points.length - 1])[0];
console.log(maxVal);

// Now, filter the array with the highest elements
let highest = points.filter(x => Object.values(x)[0] === maxVal);
console.log(highest)

如果您的初始数组未如您所提到的那样排序,那么您可以进行如下操作:

var kiara = 1;
var rodeo = 3;
var calypso = 3;
var balthazar = 3;
var mistral = 4;
var saya = 4;
var luna = 4;

var points = [{saya}, {rodeo}, {mistral}, {balthazar}, {luna}, {kiara}, {calypso}];

// Since array is not ordered, we need to get highest value.
// So, we can get it like this:
let maxVal = points.reduce((max, v) => Math.max(Object.values(v)[0], max), null);
console.log(maxVal);

// Now, filter the array with the highest elements
let highest = points.filter(x => Object.values(x)[0] === maxVal);
console.log(highest)

答案 1 :(得分:1)

一种方法是首先将.map.reduce协同工作,找到最大的价值。然后,我们可以从points数组中获得具有其第一个属性,其值为maxValue的对象,并将其推入单独的数组newArray

var maxValue = points
    .map(function(obj){ return obj[Object.keys(obj)[0]]; }) //map into array of numbers
    .reduce(function(a, b){ return Math.max(a, b);}); // return max value

var newArray = points // filter objects with first prop = maxValue
    .filter(function(obj){ return obj[Object.keys(obj)[0]] === maxValue; });

结果将是: [{mistral: 4}, {saya: 4}, {luna: 4}]

答案 2 :(得分:0)

下面的解决方案。可以使用更少的代码来完成此操作,但这将为您完成工作。要记住一件事,因为在循环中使用对象时是您的朋友。

left

:)

答案 3 :(得分:0)

这是ES6的处理方式。列表的边缘情况不按顺序处理,因此最大值可以在任何地方。您也可以对其进行修改以获取最小值。如果您对代码有任何疑问,请询问,我会答复。

const kiara = 1;
const rodeo = 3;
const calypso = 3;
const balthazar = 3;
const mistral = 4;
const saya = 4;
const luna = 4;


function getNamesWithHighestPoints(points) {
  const hash = {};

  for(let i = 0; i < points.length; i++) {

    let point = points[i];
    let name = Object.keys(point)[0];
    let score = Object.values(point)[0];

    if(hash[score] === undefined) {
      hash[score] = [point];
    }
    else {
      hash[score].push(point);
    }
  }

  let biggestScore = 0;
  Object.keys(hash).forEach(pointKey => {
    if(biggestScore < pointKey) {
      biggestScore = pointKey;
    }
  });
  return hash[biggestScore];
}
const points = [{kiara}, {rodeo}, {calypso}, {balthazar}, {mistral}, {saya}, {luna}];

console.log(getNamesWithHighestPoints(points));