遍历对象数组-Javascript

时间:2018-07-31 01:46:02

标签: javascript

// Hi, I am having following array of data
const MyCorp = [{
    firstName: "ABC Corp",
    ownShareholders: true,
    percentageOwnership: 50,
    shareholders: [{
        firstName: "XYZ Corp",
        percentageOwnership: 50,
        ownShareholders: false,
        shareholders: []
      },
      {
        firstName: "DEF Corp",
        ownShareholders: true,
        percentageOwnership: 60,
        shareholders: [{
            firstName: "SAY Corp",
            percentageOwnership: 50,
            ownShareholders: false,
            shareholders: []
          },
          {
            firstName: "No Corp",
            ownShareholders: false,
            percentageOwnership: 50,
            shareholders: []
          }
        ]
      }
    ]
  },
  {
    firstName: "Test Corp",
    ownShareholders: false,
    shareholders: [],
    percentageOwnership: 50
  }
]

// A trying to iterate over the data to get below output

// MyCorp share value = 50+ 50 == 100 (50 is from ABC Corp and  50 is of Test Corp)
// ABC Corp share value = 50 + 60 ===110(50 is from XYZ Corp and  60 is of DEF Corp)
// DEF Corp Share value = 50+ 50 === 100(50 is from SAY Corp and 50 is of No Corp)
// Output can be a object or array

// What I did to achieve this 

const ShareArray = (shareholders) => {
  const something = [];
  shareholders.forEach((shareholder) => {
    if (shareholder.ownShareholders === true) {
      something.push(shareholder.shareholders);
    }
  });
  return validateSharePercentage(something[0])
};

const validateSharePercentage = shareholders => {
  let value = 0;
  shareholders.forEach(shareholder => {
    value += shareholder.percentageOwnership;
  });
  return value;
};
const somevalue = validateSharePercentage(MyCorp);
console.log(somevalue);
const arrayValues = ShareArray(MyCorp);
console.log(arrayValues);

我得到100和110,但不确定如何遍历所有子项并获得单个公司的总股本

子股东可能会根据用户需求增长-似乎我需要实现递归功能,但我也未能实现 请帮忙... 谢谢

2 个答案:

答案 0 :(得分:0)

您可以先将MyCorp数组放到一个新数组中,并与其他数据一样将firstNameshareholders的对象与其他数据一样,以便可以使用递归算法。

然后,只需收集每个股东的percentageOwnership并在shareholders数组上递归调用函数即可。

示例

const MyCorp = [
  {
    firstName: "ABC Corp",
    ownShareholders: true,
    percentageOwnership: 50,
    shareholders: [
      {
        firstName: "XYZ Corp",
        percentageOwnership: 50,
        ownShareholders: false,
        shareholders: []
      },
      {
        firstName: "DEF Corp",
        ownShareholders: true,
        percentageOwnership: 60,
        shareholders: [
          {
            firstName: "SAY Corp",
            percentageOwnership: 50,
            ownShareholders: false,
            shareholders: []
          },
          {
            firstName: "No Corp",
            ownShareholders: false,
            percentageOwnership: 50,
            shareholders: []
          }
        ]
      }
    ]
  },
  {
    firstName: "Test Corp",
    ownShareholders: false,
    shareholders: [],
    percentageOwnership: 50
  }
];

const data = [
  {
    firstName: "MyCorp",
    shareholders: MyCorp
  }
];

const result = data.reduce(function getPercentageOwnership(result, element) {
  const { firstName } = element;

  if (element.shareholders.length !== 0) {
    result[firstName] = 0;
  }
  element.shareholders.forEach(shareholder => {
    result[firstName] += shareholder.percentageOwnership;
    getPercentageOwnership(result, shareholder);
  });

  return result;
}, {});

console.log(result);

答案 1 :(得分:0)

我将MyCorp添加到相同的结构(名字,ownShareholders,percentOwnership,股东)

$pattern = <div class="col-md-3 product-c"><span (?:.*?)>([0-9]+)</div>