使用javascript从JSON获取*值的前4个键和值的2个不同数组

时间:2022-01-09 12:16:28

Let's say I have a JSON object.

假设我有一个JSON对象。

var JSONObj = "skills":[
            {
                "skill": "HTML",
                "rate": "4"
            },
            {
                "skill": "JS",
                "rate": "5"
            },
            {
                "skill": "jQuery",
                "rate": "8"
            },
            {
                "skill": "Angular 2",
                "rate": "1"
            },
            {
                "skill": "XML",
                "rate": "3"
            },
            {
                "skill": "CSS",
                "rate": "10"
            }
        ]

I want to generate 2 different arrays for TopRates and TopSkills based on their rate in Javascript. e.g, var TopSkills = ["CSS", "jQuery", "JS", "HTML"]; var TopRates = [10, 8, 5, 4]

我想基于他们在Javascript中的速率为TopRates和TopSkills生成2个不同的数组。例如,var TopSkills = [“CSS”,“jQuery”,“JS”,“HTML”]; var TopRates = [10,8,5,4]

4 个解决方案

#1


1  

You can use reduce() to return arrays of skills and rates and then slice() to return top 4.

您可以使用reduce()返回技能和速率数组,然后使用slice()返回前4位。

var JSONObj = {
  "skills": [{
    "skill": "HTML",
    "rate": "4"
  }, {
    "skill": "JS",
    "rate": "5"
  }, {
    "skill": "jQuery",
    "rate": "8"
  }, {
    "skill": "Angular 2",
    "rate": "1"
  }, {
    "skill": "XML",
    "rate": "3"
  }, {
    "skill": "CSS",
    "rate": "10"
  }]
}

var r = JSONObj.skills.sort((a, b) => b.rate - a.rate).reduce(function(r, e) {
  r.skills = (r.skills || []).concat(e.skill);
  r.rates = (r.rates || []).concat(e.rate);
  return r;
}, {})

var TopSkills = r.skills.slice(0, 4);
var TopRates = r.rates.slice(0, 4);
console.log(TopSkills)
console.log(TopRates)

#2


0  

You can sort the array by rate first, then map with skill and rate:

您可以先按速率对数组进行排序,然后按照技巧和速率进行映射:

var sortedBySkill = JSONObj.skills.sort(function(a, b) {
    return b.rate - a.rate;
});

console.log(sortedBySkill.map(function(a) {
    return a.skill;
}).slice(0,4));

console.log(sortedBySkill.map(function(a) {
    return a.rate;
}).slice(0,4));

Here's the fiddle. Hope it helps.

这是小提琴。希望能帮助到你。

#3


0  

or even easily with forEach

或者甚至很容易与forEach

var topSkills = [];
var topRates = [];
JSONObj.skills.sort((a, b) => b.rate - a.rate).forEach(item => {
  topSkills.push(item.skill);
  topRates.push(item.rate);
});

#4


0  

var JSONObj = [{"skill":"HTML","rate":"4"},{"skill":"JS","rate":"5"},{"skill":"jQuery","rate":"8"},{"skill":"Angular 2","rate":"1"},{"skill":"XML","rate":"3"},{"skill":"CSS","rate":"10"}]

JSONObj.sort(function(a,b){return  parseFloat(a.rate) - parseFloat(b.rate)});
JSONObj.reverse();

var TopSkills=[];
var TopRates=[];

for(var i=0;i<JSONObj.length;i++){
  TopRates.push(JSONObj[i].rate);
  TopSkills.push(JSONObj[i].skill);
}

Check demo

#1


1  

You can use reduce() to return arrays of skills and rates and then slice() to return top 4.

您可以使用reduce()返回技能和速率数组,然后使用slice()返回前4位。

var JSONObj = {
  "skills": [{
    "skill": "HTML",
    "rate": "4"
  }, {
    "skill": "JS",
    "rate": "5"
  }, {
    "skill": "jQuery",
    "rate": "8"
  }, {
    "skill": "Angular 2",
    "rate": "1"
  }, {
    "skill": "XML",
    "rate": "3"
  }, {
    "skill": "CSS",
    "rate": "10"
  }]
}

var r = JSONObj.skills.sort((a, b) => b.rate - a.rate).reduce(function(r, e) {
  r.skills = (r.skills || []).concat(e.skill);
  r.rates = (r.rates || []).concat(e.rate);
  return r;
}, {})

var TopSkills = r.skills.slice(0, 4);
var TopRates = r.rates.slice(0, 4);
console.log(TopSkills)
console.log(TopRates)

#2


0  

You can sort the array by rate first, then map with skill and rate:

您可以先按速率对数组进行排序,然后按照技巧和速率进行映射:

var sortedBySkill = JSONObj.skills.sort(function(a, b) {
    return b.rate - a.rate;
});

console.log(sortedBySkill.map(function(a) {
    return a.skill;
}).slice(0,4));

console.log(sortedBySkill.map(function(a) {
    return a.rate;
}).slice(0,4));

Here's the fiddle. Hope it helps.

这是小提琴。希望能帮助到你。

#3


0  

or even easily with forEach

或者甚至很容易与forEach

var topSkills = [];
var topRates = [];
JSONObj.skills.sort((a, b) => b.rate - a.rate).forEach(item => {
  topSkills.push(item.skill);
  topRates.push(item.rate);
});

#4


0  

var JSONObj = [{"skill":"HTML","rate":"4"},{"skill":"JS","rate":"5"},{"skill":"jQuery","rate":"8"},{"skill":"Angular 2","rate":"1"},{"skill":"XML","rate":"3"},{"skill":"CSS","rate":"10"}]

JSONObj.sort(function(a,b){return  parseFloat(a.rate) - parseFloat(b.rate)});
JSONObj.reverse();

var TopSkills=[];
var TopRates=[];

for(var i=0;i<JSONObj.length;i++){
  TopRates.push(JSONObj[i].rate);
  TopSkills.push(JSONObj[i].skill);
}

Check demo