当前位置: 代码迷 >> JavaScript >> 如何根据变量的值从数组中提取变量?
  详细解决方案

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

热度:39   发布时间:2023-06-12 14:03:41.0

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

这是为我女儿在 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 个变量(变量名称 + 各自的值)并将它们存储在另一个数组中。 如果我遇到单个变量具有最高值的情况,那么单个变量将存储在新数组中。

由于您的数组是有序的,您已经知道最大值位于数组的最后一个位置。 所以首先,你可以得到这个值,然后你可以使用这个值来那些值等于这个值的元素:

 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)

一种方法是首先使用.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}]

解决方法如下。 有一种方法可以用更少的代码来做到这一点,但这将为您完成工作。 要记住的一件事是,for in 循环是处理对象时的朋友。

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

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

var maxNumberArray = [];
var maxNamesArray = []; // ADDED THIS LINE
var max;

for(var char in points){
    for(var name in points[char]){
        if(!max){
            max = points[char][name];
        } else {
            if(max < points[char][name]){
                max = points[char][name];
            }
        }
    }
}

for(var char in points){
    for(var name in points[char]){
        if(points[char][name] === max){
            maxNumberArray.push(points[char]);
            maxNamesArray.push(name); // ADDED THIS LINE
        }
    }
}



console.log(JSON.stringify(maxNumberArray));
console.log(maxNamesArray); // ADDED this LINE

:)

这是 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));

  相关解决方案