js问题求助

var arr = [
    {
        num : 33,
        str: "one"
    },
    {
        num : 34,
        str: "two"
    },
    {
        num : 36,
        str: "three"
    },
    {
        num : 37,
        str: "一"
    },
    {
        num : 42,
        str: "二"
    }
]

想得到

var newarr = [
    ["one","two"],
    ["three","一"],
    ["二"]
]

相邻的放在一个数组里面 33,34 36,37 42 求助js该怎么写

阅读 2.9k
6 个回答

其实原理很简单,咱就比比谁代码量少吧。

var arr = [
    {
        num : 33,
        str: "one"
    },
    {
        num : 34,
        str: "two"
    },
    {
        num : 36,
        str: "three"
    },
    {
        num : 37,
        str: "一"
    },
    {
        num : 42,
        str: "二"
    }
];

var nowNum = -1;
var newArr = [];

arr.forEach(function (x) {
  if (x.num === nowNum + 1) {
    newArr[newArr.length - 1].push(x.str);
  } else {
    newArr.push([x.str]);
    nowNum = x.num;
  }
});
console.log(newArr);

第一次回答。。。

var arr = [
  {
      num : 33,
      str: "one"
  },
  {
      num : 34,
      str: "two"
  },
  {
      num : 36,
      str: "three"
  },
  {
      num : 37,
      str: "一"
  },
  {
      num : 42,
      str: "二"
  }
]
var num = ''
var res = []
var ars = []
for (var i = 0; i < arr.length; i ++) {
  now = arr[i].num
  if (now !== num + 1 && ars.length > 0) {
    res.push(ars)
    ars = []
  }
  ars.push(arr[i].str)
  if (i === arr.length - 1) {
    res.push(ars)
  }
  num = now
}
console.log(res);

考虑到初始数组里的顺序可能是乱的所以先提成了一个数组。

function toNewArr(arr) {
            var newArr = [];
            var tempArr = []
            //提成数组
            for(var i = 0; i < arr.length; i++) {
                tempArr[arr[i].num] = arr[i].str
            }
            //实现部分
            for(var j = 0, nth = 0, flag = false; j < tempArr.length; j++) {
                if(flag === false) {
                    //子项置为数组类型
                    newArr[nth] = [];
                    flag = true;
                    console.log('!!',flag)
                };
                //不为空的索引项加到新的数组的子数组中
                if(tempArr[j] !== undefined) {
                    newArr[nth].push(tempArr[j]);
                    //子数组长度为2,索引加1,且新子项需置为数组类型
                    if(newArr[nth].length === 2) {
                        nth++;
                        flag = false;
                    }
                }
            }
            return newArr;
        }

试试这个

var arr = [{
  num: 34,
  str: "two"
}, {
  num: 33,
  str: "one"
}, {
  num: 36,
  str: "three"
}, {
  num: 37,
  str: "一"
}, {
  num: 42,
  str: "二"
}];

function split(arr) {
  let res = [];
  let tmp = [];
  arr.sort((a, b) => a.num - b.num).forEach((v, i) => {
    if (i <= 0) return tmp.push(v.str);
    if (arr[i - 1].num + 1 === v.num) return tmp.push(v.str);
    res.push(tmp);
    tmp = [v.str];
  });
  if (tmp.length > 0) res.push(tmp);

  return res;
}

console.log(split(arr));
var arr = [
    {
        num : 33,
        str: "one"
    },
    {
        num : 34,
        str: "two"
    },
    {
        num : 36,
        str: "three"
    },
    {
        num : 37,
        str: "一"
    },
    {
        num : 42,
        str: "二"
    }
];

var rel = getRel(arr);

console.log(rel); // 这就是你想要的结果

function getRel(arr){
    var i;
    var truncate = 2;
    var rel      = [];
    var tempArr  = [];
    
    for (i = 0; i < arr.length; ++i)
        {    
            tempArr.push(arr[i]['str']);
            
            // 每 truncate 数量,添加至 rel + 重置 tempVal
            if ((i + 1) % truncate === 0) {
                 rel.push(tempArr);
                 tempArr = [];
            }
        }
    
    // 如果数组的长度不能被 truncate 除尽的时候
    // 即使凑不成 truncate 数量,仍然扔进结果数组里面
    if (arr.length % truncate !== 0) {
        rel.push(tempArr);
    }
    
    return rel;
}
var arr = [
    {
        num : 33,
        str: "one"
    },
    {
        num : 34,
        str: "two"
    },
    {
        num : 36,
        str: "three"
    },
    {
        num : 37,
        str: "一"
    },
    {
        num : 42,
        str: "二"
    }
]
let targetArr = arr.reduce(function(pre,cur){
     return pre.concat(cur.str)
},[])
function splitArrMember(arr,n) {
        let arr_length = arr.length, result=[];
        if (arr_length <= n) {
        return arr;
    }
    let arrDivide = arr_length / n + 1;
    if (Number.isInteger(arrDivide)) {
         arrDivide--
    }
    arrDivide = Math.floor(arrDivide);
    for(let i =0;i<arrDivide;i++) {
        let targetMem = i*n;
        result.push(arr.slice(targetMem,n+targetMem))
    }
    return result
}
splitArrMember(targetArr,2)
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题