模块化方法中的Javascript对象排序和添加新属性

模块化方法中的Javascript对象排序和添加新属性,javascript,coding-style,Javascript,Coding Style,我有以下javascript对象 mData=[{A: "148.0", Bit: 27 ,Ic: "0.4",ked: "229.0",Ted: "228.9985"}, {A: "148.0", Bit: 27 ,Ic: "0.4",ked: "229.0",Ted: "2285"}, {A: "148.0", Bit: 17 ,Ic: "0.4",ked: "259.0",Ted: "28"}, {A: "148.0", Bit: 27 ,Ic: "0.4",ked: "279.0",Te

我有以下javascript对象

mData=[{A: "148.0", Bit: 27 ,Ic: "0.4",ked: "229.0",Ted: "228.9985"},
{A: "148.0", Bit: 27 ,Ic: "0.4",ked: "229.0",Ted: "2285"},
{A: "148.0", Bit: 17 ,Ic: "0.4",ked: "259.0",Ted: "28"},
{A: "148.0", Bit: 27 ,Ic: "0.4",ked: "279.0",Ted: "28"},             
{A: "148.0", Bit: 27 ,Ic: "0.4",ked: "239.0",Ted: "82"},
{A: "148.0", Bit: 17 ,Ic: "0.4",ked: "219.0",Ted: "22"},
{A: "148.0", Bit: 16 ,Ic: "0.4",ked: "239.0",Ted: "22"},      
{A: "148.0", Bit: 17 ,Ic: "0.4",ked: "259.0",Ted: "22"}];
我试图根据它们的位属性将它们分开。我提出了如下非常简单的算法,但它既不是模块化的,也不是泛型的。如何使下面的代码更通用

color=["red","blue","green"];

data1=[];data2=[];data3=[];

$.each(mData, function (i, wData){
  if(wData.Bit===27)
  {
    wData.color=color[0];
    data1.push(wData);
  }
  else if(wData.Bit===17)
  {
    wData.color=color[1];
    data2.push(wData);
  }
   else
  {
    wData.color=color[2];
    data3.push(wData);
  }
});

除此之外,我想在推送相应数据之前为每个对象添加一个颜色属性,比如说
如果BIT>27 color='red',如果BIT>17 color='blue',如果BIT有趣的问题!这实际上是两个问题,让我一次回答一个

如何按特定字段对对象进行分组? 您尝试执行的操作类型是分组操作。让我们从消除位值的硬编码检查开始。为此,我们首先要将输出更改为单个对象,而不是多个数组。然后,这些对象可以将位值作为键,并将包含具有该位值的所有对象的数组作为值。例如:

result = {
    17 : [ /* All objects with Bit=17 */],
    27 :  [ /* All objects with bit 27 */],
    //etc
};
让我们试试看:

function orderByBit(data) {
    //let's start with an empty object.
    var result = {};
    //array.forEach is similar to $.each(array), but then built-in all
    //non-ancient browsers (that is, >= IE9)
    data.forEach(function(item) {
        var bit = item.Bit;
        //First we check if we already have a array created for the bit value.
        //if not, then we create one
        if (!result[bit]) {
            result[bit] = [];
        }
        //Now we just have to push the item to the correct array
        result[bit].push(item)
    });
    //Done!
    return result;
}

var orderedData = orderByBit(mData);
但我们可以做得更好。我们现在仍然有硬编码,我们希望根据
位的值对其进行分组,但我们可能也希望改变这一点。为此,我们可以要求将字段分组为参数

function orderBy(field, data) {
    //let's start again with an empty object.
    var result = {};
    data.forEach(function(item) {
        //We no longer hardcode the Bit field, instead we use the field
        //passed in as argument
        var fieldValue = item[field];
        //The rest is the same, but now with fieldValue instead of bit
        if (!result[fieldValue]) {
            result[fieldValue] = [];
        }
        result[fieldValue].push(item)
    });
    //Done!
    return result;
}

var orderdData = orderBy("Bit", mData);
现在回答第二个问题:

如何向所有对象添加颜色特性 在您的情况下,如果要根据范围添加值,您可以迭代所有对象并在循环中执行值检查:

mData.forEach(function(item) {
    if (item.Bit < 17) {
        item.color = 'yellow';
    } else if (item.Bit < 27) {
        item.color = 'blue';
    } else {
        item.color = 'red';
    }
});
//and then again ordering
var orderdData = orderBy("Bit", mData);
mData.forEach(函数(项){
如果(项目位<17){
item.color='黄色';
}否则,如果(项目位<27){
item.color='蓝色';
}否则{
item.color='红色';
}
});
//然后再次订购
var orderdData=orderBy(“位”,mData);

更新:

有趣的问题!这实际上是两个问题,让我一次回答一个

如何按特定字段对对象进行分组? 您尝试执行的操作类型是分组操作。让我们从消除位值的硬编码检查开始。为此,我们首先要将输出更改为单个对象,而不是多个数组。然后,这些对象可以将位值作为键,并将包含具有该位值的所有对象的数组作为值。例如:

result = {
    17 : [ /* All objects with Bit=17 */],
    27 :  [ /* All objects with bit 27 */],
    //etc
};
让我们试试看:

function orderByBit(data) {
    //let's start with an empty object.
    var result = {};
    //array.forEach is similar to $.each(array), but then built-in all
    //non-ancient browsers (that is, >= IE9)
    data.forEach(function(item) {
        var bit = item.Bit;
        //First we check if we already have a array created for the bit value.
        //if not, then we create one
        if (!result[bit]) {
            result[bit] = [];
        }
        //Now we just have to push the item to the correct array
        result[bit].push(item)
    });
    //Done!
    return result;
}

var orderedData = orderByBit(mData);
但我们可以做得更好。我们现在仍然有硬编码,我们希望根据
位的值对其进行分组,但我们可能也希望改变这一点。为此,我们可以要求将字段分组为参数

function orderBy(field, data) {
    //let's start again with an empty object.
    var result = {};
    data.forEach(function(item) {
        //We no longer hardcode the Bit field, instead we use the field
        //passed in as argument
        var fieldValue = item[field];
        //The rest is the same, but now with fieldValue instead of bit
        if (!result[fieldValue]) {
            result[fieldValue] = [];
        }
        result[fieldValue].push(item)
    });
    //Done!
    return result;
}

var orderdData = orderBy("Bit", mData);
现在回答第二个问题:

如何向所有对象添加颜色特性 在您的情况下,如果要根据范围添加值,您可以迭代所有对象并在循环中执行值检查:

mData.forEach(function(item) {
    if (item.Bit < 17) {
        item.color = 'yellow';
    } else if (item.Bit < 27) {
        item.color = 'blue';
    } else {
        item.color = 'red';
    }
});
//and then again ordering
var orderdData = orderBy("Bit", mData);
mData.forEach(函数(项){
如果(项目位<17){
item.color='黄色';
}否则,如果(项目位<27){
item.color='蓝色';
}否则{
item.color='红色';
}
});
//然后再次订购
var orderdData=orderBy(“位”,mData);

更新:

可能像这样使用reduce

var create_map = function(mData, colors, indexes) {
    return mData.reduce( function(res, curr) {
       var i = indexes[curr.Bit] || 0;
       curr.color = colors[i];
       ( res[i] = res[i] || [] ).push(curr); 
       return res;
    },[]);
}
// define colors and bits which to place into index
var colors = ["red","blue","green"],
    indexes = { 17 : 1, 27 : 2}; // zero default, 17 => index 1
var res = create_map( mData, colors, indexes );  
// res[0] = array of all
// res[1] = array of Bits == 17
// res[2] = array of Bits == 27

演示就在这里

可能使用了类似于这样的reduce

var create_map = function(mData, colors, indexes) {
    return mData.reduce( function(res, curr) {
       var i = indexes[curr.Bit] || 0;
       curr.color = colors[i];
       ( res[i] = res[i] || [] ).push(curr); 
       return res;
    },[]);
}
// define colors and bits which to place into index
var colors = ["red","blue","green"],
    indexes = { 17 : 1, 27 : 2}; // zero default, 17 => index 1
var res = create_map( mData, colors, indexes );  
// res[0] = array of all
// res[1] = array of Bits == 17
// res[2] = array of Bits == 27
演示就在这里

如果你知道你要找的是哪一位#,
数组
原型有一个函数,你可以使用这个函数

var twentyseven = mData.filter(function(el, i, arr) {return el.Bit === 27;});
但是,如果您想动态地将它们拆分为数组,而不需要事先知道位,则可以使用
数组
原型的功能:

var bitArrays = {};
mData.forEach(function(el, i, arr){
    if(!bitArrays[el.Bit]){
        bitArrays[el.Bit] = [];
    } 
    bitArrays[el.Bit].push(el);
});
这种方法将为您提供一个对象,该对象包含每个位的数组。例如,您可以通过访问
位数组[27]

下面是一些工作示例代码:

//要登录到HTML的帮助程序代码
变量日志=(函数(输出){
返回函数(输入){
insertAdjacentHTML(“beforeend”,input);
}
})(document.getElementById(“输出”);
变量位数组={};
var mData=[{A:“148.0”,位:27,Ic:“0.4”,ked:“229.0”,Ted:“228.9985”},
{A:“148.0”,位:27,Ic:“0.4”,ked:“229.0”,Ted:“2285”},
{A:“148.0”,位:17,Ic:“0.4”,ked:“259.0”,Ted:“28”},
{A:“148.0”,位:27,Ic:“0.4”,ked:“279.0”,Ted:“28”},
{A:“148.0”,位:27,Ic:“0.4”,ked:“239.0”,Ted:“82”},
{A:“148.0”,位:17,Ic:“0.4”,ked:“219.0”,Ted:“22”},
{A:“148.0”,位:16,Ic:“0.4”,ked:“239.0”,Ted:“22”},
{A:“148.0”,比特:17,Ic:“0.4”,ked:“259.0”,Ted:“22”}];
mData.forEach(函数(el、i、arr){
if(!位数组[el.Bit]){
位数组[el.Bit]=[];
} 
位数组[el.Bit].push(el);
});
日志(JSON.stringify(位数组))
如果您知道要查找哪一位,那么
阵列
原型有一个名为的函数,可以用于此目的

var twentyseven = mData.filter(function(el, i, arr) {return el.Bit === 27;});
但是,如果您想动态地将它们拆分为数组,而不需要事先知道位,则可以使用
数组
原型的功能:

var bitArrays = {};
mData.forEach(function(el, i, arr){
    if(!bitArrays[el.Bit]){
        bitArrays[el.Bit] = [];
    } 
    bitArrays[el.Bit].push(el);
});
这种方法将为您提供一个对象,该对象包含每个位的数组。例如,您可以通过访问
位数组[27]

下面是一些工作示例代码:

//要登录到HTML的帮助程序代码
变量日志=(函数(输出){
返回函数(输入){
insertAdjacentHTML(“beforeend”,input);
}
})(document.getElementById(“输出”);
变量位数组={};
var mData=[{A:“148.0”,位:27,Ic:“0.4”,ked:“229.0”,Ted:“228.9985”},
{A:“148.0”,位:27,Ic:“0.4”,ked:“229.0”,Ted:“2285”},
{A:“148.0”,位:17,Ic:“0.4”,ked:“259.0”,Ted:“28”},
{A:“148.0”,位:27,Ic:“0.4”,ked:“279.0”,Ted:“28”},
{A:“148.0”,位:27,Ic:“0.4”,ked:“239.0”,Ted:“82”},
{A:“148.0”,位:17,Ic:“0.4”,ked:“219.0”,Ted:“22”},
{A:“148.0”,位:16,Ic:“0.4”,ked:“239.0”,Ted:“22”},
{A:“148.0”,比特:17,Ic:“0.4”,ked:“259.0”,Ted:“22”}];
mData.forEach(函数(el、i、arr){
if(!位数组[el.Bit]){
位数组[el.Bit]=[];
} 
位数组[el.Bit].push(el);
});
日志(JSON.stringify(位数组))