账号密码登录
微信安全登录
微信扫描二维码登录

登录后绑定QQ、微信即可实现信息互通

手机验证码登录
找回密码返回
邮箱找回 手机找回
注册账号返回
其他登录方式
分享
  • 收藏
    X
    在JavaScript中一组数据如何进行关联呢
    50
    0
    我有以下数据 const data = [ { id: 56, parentId: 62, text: "56" }, { id: 74, parentId: null, text: "74" }, { id: 76, parentId: 80, text: "76" }, { id: 63, parentId: 62, text: "63" }, { id: 80, parentId: 86, text: "80" }, { id: 62, parentId: 74, text: "62" }, { id: 86, parentId: 74, text: "86" }, ];我的尝试 const result = []; const saveMap = {}; const rootIndex = data.findIndex((item) => { const { parentId } = item; return parentId === null; }); saveMap[rootIndex] = data[rootIndex].id for (let i = 0; i < data.length; i++) { for (let j = 0; j < data.length; j++) { const root = data[rootIndex]; const { id } = root; if (id == data[j].id) { continue } result.push({source:root.id,target:data[j].id}) saveMap[j] = data[j].id } } console.log(result);emmm,不是我想要的结构。。。我想要数据:const result = [ {source:74,target:62}, {source:74,target:86}, {source:62,target:56}, {source:62,target:63}, {source:86,target:80}, {source:80,target:76}, ... //依次类推]感谢您的帮助.
    0
    打赏
    收藏
    点击回答
        全部回答
    • 0
    • 芋头焖排骨 普通会员 1楼

      在JavaScript中,一组数据可以通过数组或者对象进行关联。以下是一个使用数组进行关联的例子:

      ```javascript let data = [ { name: 'John', age: 30, city: 'New York' }, { name: 'Jane', age: 25, city: 'Los Angeles' }, { name: 'Bob', age: 40, city: 'Chicago' } ];

      let connectedData = data.filter(item => item.name === 'John');

      console.log connectedData); // 输出:[ { name: 'John', age: 30, city: 'New York' }, { name: 'John', age: 40, city: 'Chicago' } ] ```

      在这个例子中,我们使用了filter函数和一个比较操作符===来创建一个新数组,这个新数组只包含名字为'John'的数据。我们使用map函数来遍历这个新数组,并使用filter函数来过滤出名字为'John'的数据。

      如果我们想要将数据通过对象进行关联,我们可以这样做:

      ```javascript let data = [ { name: 'John', age: 30, city: 'New York' }, { name: 'Jane', age: 25, city: 'Los Angeles' }, { name: 'Bob', age: 40, city: 'Chicago' } ];

      let connectedData = data.reduce((acc, item) => { if (item.name === 'John') { acc.push(item); } return acc; }, []);

      console.log connectedData); // 输出:[ { name: 'John', age: 30, city: 'New York' }, { name: 'John', age: 40, city: 'Chicago' } ] ```

      在这个例子中,我们使用了reduce函数来遍历这个数据,并在每次迭代中检查当前元素是否是名字为'John'的数据。如果是,我们就将这个数据添加到结果数组中。如果当前元素不是名字为'John'的数据,我们就返回现有的结果数组。

    • 妳°幼稚鬼 普通会员 2楼

      在JavaScript中,一组数据可以通过数组或者对象进行关联。以下是一个使用数组进行关联的例子:

      ```javascript let data = [ { name: 'John', age: 30, city: 'New York' }, { name: 'Jane', age: 25, city: 'Los Angeles' }, { name: 'Bob', age: 40, city: 'Chicago' } ];

      let connectedData = data.filter(item => item.name === 'John');

      console.log connectedData); // 输出:[ { name: 'John', age: 30, city: 'New York' }, { name: 'John', age: 40, city: 'Chicago' } ] ```

      在这个例子中,我们使用了filter函数和一个比较操作符===来创建一个新数组,这个新数组只包含名字为'John'的数据。我们使用map函数来遍历这个新数组,并使用filter函数来过滤出名字为'John'的数据。

      如果我们想要将数据通过对象进行关联,我们可以这样做:

      ```javascript let data = [ { name: 'John', age: 30, city: 'New York' }, { name: 'Jane', age: 25, city: 'Los Angeles' }, { name: 'Bob', age: 40, city: 'Chicago' } ];

      let connectedData = data.reduce((acc, item) => { if (item.name === 'John') { acc.push(item); } return acc; }, []);

      console.log connectedData); // 输出:[ { name: 'John', age: 30, city: 'New York' }, { name: 'John', age: 40, city: 'Chicago' } ] ```

      在这个例子中,我们使用了reduce函数来遍历这个数据,并在每次迭代中检查当前元素是否是名字为'John'的数据。如果是,我们就将这个数据添加到结果数组中。如果当前元素不是名字为'John'的数据,我们就返回现有的结果数组。

    • ωo㊣②⑧經愛鉨 普通会员 3楼

      在JavaScript中,一组数据可以通过数组或者对象进行关联。以下是一个使用数组进行关联的例子:

      ```javascript let data = [ { name: 'John', age: 30, city: 'New York' }, { name: 'Jane', age: 25, city: 'Los Angeles' }, { name: 'Bob', age: 40, city: 'Chicago' } ];

      let connectedData = data.filter(item => item.name === 'John');

      console.log connectedData); // 输出:[ { name: 'John', age: 30, city: 'New York' }, { name: 'John', age: 40, city: 'Chicago' } ] ```

      在这个例子中,我们使用了filter函数和一个比较操作符===来创建一个新数组,这个新数组只包含名字为'John'的数据。我们使用map函数来遍历这个新数组,并使用filter函数来过滤出名字为'John'的数据。

      如果我们想要将数据通过对象进行关联,我们可以这样做:

      ```javascript let data = [ { name: 'John', age: 30, city: 'New York' }, { name: 'Jane', age: 25, city: 'Los Angeles' }, { name: 'Bob', age: 40, city: 'Chicago' } ];

      let connectedData = data.reduce((acc, item) => { if (item.name === 'John') { acc.push(item); } return acc; }, []);

      console.log connectedData); // 输出:[ { name: 'John', age: 30, city: 'New York' }, { name: 'John', age: 40, city: 'Chicago' } ] ```

      在这个例子中,我们使用了reduce函数来遍历这个数据,并在每次迭代中检查当前元素是否是名字为'John'的数据。如果是,我们就将这个数据添加到结果数组中。如果当前元素不是名字为'John'的数据,我们就返回现有的结果数组。

    更多回答
    扫一扫访问手机版
    • 回到顶部
    • 回到顶部