亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

減少嵌套數組中的元素

減少嵌套數組中的元素

牛魔王的故事 2023-11-02 21:55:57
我正在嘗試過濾數組并減少值,但我不知道如何操作。所以,我有一個示例數組[["Hampton Tricep Rope", 3],["Chrome Curl" Bar,8],["Hampton Tricep Rope", 6]]如何創建一個帶有 return 的函數[["Hampton Tricep Rope", 9],["Chrome Curl" Bar,8]]?你有什么主意嗎?
查看完整描述

3 回答

?
眼眸繁星

TA貢獻1873條經驗 獲得超9個贊

您可以像這樣將列表獲取到頁面。然后您可以在每個循環中按 div 或 ul 列表內部。


function findEmployees(userCounty) {

        $.ajax({

            type: "POST",

            dataType: "json",

            url: '@Url.Action("getCounty", "Contact")',

            data: JSON.stringify(userCounty),

            contentType: "application/json",

            success: function (result) {

                if (result.data.length !== 0) {

                    $.each(result.data, function (index, value) {

                        var firstName = value.firstName;

                        var lastName = value.lastName;

                    });

                }

            },

        });

    }


對象.條目


查看完整回答
反對 回復 2023-11-02
?
富國滬深

TA貢獻1790條經驗 獲得超9個贊

您可以使用reduce和findIndex


const list = [

  ["Hampton Tricep Rope", 3],

  ["Chrome Curl Bar", 8],

  ["Hampton Tricep Rope", 6]

].reduce((acc, x) => {

  const index = acc.findIndex(y => y[0] === x[0]);

  if (index >= 0) {

    acc[index][1] += x[1];

    return acc;

  }

  acc.push(x);

  return acc;

}, [])


console.log(list)


查看完整回答
反對 回復 2023-11-02
?
慕虎7371278

TA貢獻1802條經驗 獲得超4個贊

var result = [];

[["Hampton Tricep Rope", 3],["Chrome Curl Bar",8],["Hampton Tricep Rope", 6]].reduce(function(res, value) {

  if (!result.filter((item) => (item[0] === value[0])).length) {

    result.push([value[0], value[1]]);

  } else {

    result.filter((item) => (item[0] === value[0]))[0][1] += value[1];

  }

  return res;

}, {});

我們需要構建一個數組,同時減少另一個數組,如上所示。在每一步中,我們都會檢查是否已經擁有該元素。如果沒有,那么我們添加它。否則我們根據需要增加它。


查看完整回答
反對 回復 2023-11-02
  • 3 回答
  • 0 關注
  • 158 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號