Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何使用下划线.js生成展开结果_Javascript_Json_Underscore.js - Fatal编程技术网

Javascript 如何使用下划线.js生成展开结果

Javascript 如何使用下划线.js生成展开结果,javascript,json,underscore.js,Javascript,Json,Underscore.js,json对象是 var data = [{"Parent":1,"Child":[4,5,6]},{"Parent":2},{"Parent":3}] 如何使用下划线.js链/map/pluck等。。。函数以获得展平结果 var result = []; for (var i = 0; i < data.length; i++) { result.push(data[i].Parent); if (data.Child != undefined) {

json对象是

var data = [{"Parent":1,"Child":[4,5,6]},{"Parent":2},{"Parent":3}]
如何使用下划线.js链/map/pluck等。。。函数以获得展平结果

     var result = [];
for (var i = 0; i < data.length; i++) {
    result.push(data[i].Parent);
    if (data.Child != undefined) {
        for (var j = 0; j < data[i].Child.length; j++) {
            result.push(data[i].Child[j]);
        }
    }
}
console.log(result) >> //1,4,5,6,2,3
var结果=[];
对于(变量i=0;i//1,4,5,6,2,3

假设你想先得到父母,然后再得到孩子:

_.chain(data).pluck("Parent")
             .concat(_.flatten(_(data).pluck("Child")))
             .reject(_.isUndefined)
             .value()

下面是一个简短的解决方案:

flat = _.flatten(_.map(data, _.values)) 

或者,如果您想要一个可以普遍展平任何对象或数组集合的函数

您可以使用以下命令扩展下划线: Crush(因为没有更好的名称)可以像
.Crush(list,[shall])
.Crush([shall])
一样被称为

它可以传递一组嵌套对象、数组或任意深度的嵌套对象和/或数组,并将返回一个包含所有输入值和自身属性的单层数组。与展平类似,如果传递一个计算结果为true的附加参数,则执行“浅层”执行,输出仅展平一级

例1:

例2:


代码本身的解释如下:

_.mixin({  // This extends Underscore's native object.

  crush: function(list, shallow, r) {  // The "r" is really just a fancy
                                       // way of declaring an extra variable
                                       // within the function without
                                       // taking up another line.

    return _.isObject(list)?  // Arrays (being a type of object)
                              // actually pass this test too.

      (r = function(list) {  // It doesn't matter that "r" might have
                             // been passed as an argument before,
                             // as it gets rewritten here anyway.

        return _.isObject(list)?  // While this test may seem redundant at
                                  // first, because it is enclosed in "r",
                                  // it will be useful for recursion later.

          _.flatten(_.map(list, shallow?  // Underscore's .map is different
                                          // from plain Javascript's in
          // _.map will always return     // that it will apply the passed
          // an array, which is why we    // function to an object's values
          // can then use _.flatten.      // as well as those of an array.

            _.identity  // If "shallow" is truthy, .map uses the identity
                        // function so "list" isn't altered any further.

            : r  // Otherwise, the function calls itself on each value.
          ))
          : list  // The input is returned unchanged if it has no children.
        ;
      })(list)  // The function is both defined as "r" and executed at once.

      : []  // An empty array is returned if the initial input
    ;       // was something other than an object or array.
  }
});

如果有人需要,希望它能有所帮助。:)

如果您想使用underline.js将多个数组的数组展平为一个元素数组,您可以这样做。以我为例:

我的图表有两个系列。每个系列都有一个名称和数据点序列{xtime,yValue}。我的目标是将2个系列的所有数据点整理成一个系列的数据点,以便填写表格

var reducedArray = // flatten an array of series of data-objects into one series of data-objects
_.flatten( _.map( AllMySeries, function ( aSeries ) {
    return ( _.map( aSeries.dataPoints, function ( aPoint ) {
                return { curveID: aSeries.legendText, xT: aPoint.x, yVal: aPoint.y };
            } ) );
} ) );
我的结果是:

'Series1','2017-04-19 08:54:19',1
'Series1','2017-04-19 08:59:19',0
'Series1','2017-04-19 09:04:19',1
'Series2','2017-04-19 08:54:19',1
'Series2','2017-04-19 08:59:19',0
'Series2','2017-04-19 09:04:19',1  

你到底想要实现什么?要排序的“父”数值,然后按“子”值排序?谢谢这利用了underline.js库,比我的代码简单得多。很高兴听到你喜欢它。你应该稍微玩玩一下函数式编程——它使编写这样的代码更自然,并教会你以新的方式思考。下划线只是为JavaScript提供了一系列功能特性。是否可以忽略展平?var flat=u0.map(数据,0.value)@Kuroro,
alert
不显示数组嵌套,请尝试使用
console.log
代替-您将看到map()结果是嵌套的,因此仍然需要flatte()。请注意,这还将获取数据对象中其他属性的值。只要你只有
“父”
“子”
,你就应该没事了。让我们把单子放在工作中:-)
\u.chain(data).map(\u.values).flatte().value()
像这样:.flatte(.map(data))很漂亮!谢谢
_.mixin({  // This extends Underscore's native object.

  crush: function(list, shallow, r) {  // The "r" is really just a fancy
                                       // way of declaring an extra variable
                                       // within the function without
                                       // taking up another line.

    return _.isObject(list)?  // Arrays (being a type of object)
                              // actually pass this test too.

      (r = function(list) {  // It doesn't matter that "r" might have
                             // been passed as an argument before,
                             // as it gets rewritten here anyway.

        return _.isObject(list)?  // While this test may seem redundant at
                                  // first, because it is enclosed in "r",
                                  // it will be useful for recursion later.

          _.flatten(_.map(list, shallow?  // Underscore's .map is different
                                          // from plain Javascript's in
          // _.map will always return     // that it will apply the passed
          // an array, which is why we    // function to an object's values
          // can then use _.flatten.      // as well as those of an array.

            _.identity  // If "shallow" is truthy, .map uses the identity
                        // function so "list" isn't altered any further.

            : r  // Otherwise, the function calls itself on each value.
          ))
          : list  // The input is returned unchanged if it has no children.
        ;
      })(list)  // The function is both defined as "r" and executed at once.

      : []  // An empty array is returned if the initial input
    ;       // was something other than an object or array.
  }
});
var reducedArray = // flatten an array of series of data-objects into one series of data-objects
_.flatten( _.map( AllMySeries, function ( aSeries ) {
    return ( _.map( aSeries.dataPoints, function ( aPoint ) {
                return { curveID: aSeries.legendText, xT: aPoint.x, yVal: aPoint.y };
            } ) );
} ) );
'Series1','2017-04-19 08:54:19',1
'Series1','2017-04-19 08:59:19',0
'Series1','2017-04-19 09:04:19',1
'Series2','2017-04-19 08:54:19',1
'Series2','2017-04-19 08:59:19',0
'Series2','2017-04-19 09:04:19',1