简体   繁体   English

根据特定值依次显示数组中的对象

[英]Display objects from an array in order, depending on an specific value

i have this json: 我有这个json:

var projects_array = [
{name:"proj1", category:"a", index:1},

{name:"proj2", category:"a", index:2},

{name:"proj3", category:"b", index:3},  

{name:"proj4", category:"c", index:4}
];

I am displaying them in order with something like this: 我按如下顺序显示它们:

for (var i = 0 ; i <proj_num; i++){ 
var numClass="p_"+projects_array[i].index;  
    var act="<li class='"+numClass+"'></li>";           
$('#lookfor').append(act);             
}

What i need now is to display first all the objects with the value "a" in the category attribute and then all the other objects in any order, i tried using .sort() in this way:\\ 我现在需要的是首先在category属性中显示所有值为“ a”的对象,然后按任何顺序显示所有其他对象,我尝试以这种方式使用.sort():\\

 function compare(obj){
if(obj.category == "a"){ return 1;}                 
    else{return 0;}                     
}                       
obj.sort(compare);

But it does not worked because it kind of group all the objects of the category... 但这是行不通的,因为它将某种类别的所有对象归为一类。

do you have any possible solution for this problem? 您对此问题有什么解决方案吗?

ps: I tried to simplify my code to make it understandable, i hope it be clear for you, thanks in advance ps:我试图简化代码以使其易于理解,我希望对您来说清楚一点,在此先感谢

this will sort according to the important word first. 这将首先根据重要单词进行排序。

http://jsbin.com/umezet/12/edit http://jsbin.com/umezet/12/edit

     var projects_array = [
{
    name: "proj1",
    category: "action",
    index: 1
}, {
    name: "proj2",
    category: "time",
    index: 2
}, {
    name: "proj3",
    category: "buu",
    index: 3
}, {
    name: "proj3",
    category: "time",
    index: 3
}, {
    name: "proj4",
    category: "cooo",
    index: 4
}, {
    name: "proj3",
    category: "@",
    index: 3
}, {
    name: "proj1",
    category: "!",
    index: 1
}, {
    name: "proj2",
    category: "%",
    index: 2
}, {
    name: "proj3",
    category: "cooo",
    index: 3
}, {
    name: "proj4",
    category: "*",
    index: 4
}];
projects_array = projects_array.sort(

function (x, y)
{
    if(x['category'] == 'time') return -1;
    if(y['category'] == 'time') return 1;
    if(x['category'] > y['category']) return 1;
    if(x['category'] < y['category']) return -1;
    return 0;
});
for(var i = 0; i < projects_array.length; i++)
{
    $('body').append(projects_array[i]['category']);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 根据键值对以特定顺序显示 Javascript 数组中的值 - Display values from Javascript array in specific order depending on a key value pair 如何根据键:每个对象的值组合对象数组中的特定项目,同时保持顺序? - How to combine specific items in an array of objects depending on the key: value of each object whilst maintaining the order? 按属性值按特定顺序对数组对象进行排序 - Sort array objects by property value in specific order 根据键的唯一值从对象数组中获取数据 - get data from an array of objects depending on a unique value of a key 用对象从数组中取出特定值 - Taking out specific value from array with objects 按特定顺序对对象数组进行排序 - Sorting an array of objects in a specific order 将键值对(数组中的值)添加到数组中的特定对象 - Adding key value pair (value from an array) to specific objects in an array 从对象数组中获取特定的值[{“ key”:“ value”},{“ key”:“ value”}] - Get specific value from array of objects [{“key”:“value”}, {“key”:“value”}] ReactJs:如何通过单击从对象数组中显示 object 的特定数据 - ReactJs : How to display a specific data of an object from an array of objects with a click 按值排序和截断对象数组 - Order and truncate array of objects by value
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM