返回动态对象结构

时间:2019-05-06 06:58:45

标签: javascript react-native

我在返回动态对象时遇到一些问题。我使用的是react-native chart library,它要求我像这样返回数据:

return [{
      'Manually Sold': { value: manuallySoldValue },
      'Trailing Stop Loss': { value: trailingStopLossValue },
      'Manual Short': { value: manualShortValue },
      'Profit': { value: profitValue },
      'Manual Buy': { value: manualBuy },
      'Strategy': { value: strategy },
      'Manual Close Short': { value: manualCloseShort },
      'available': { value: available },
      'in open positions': { value: inOpenPositions },
      'reserved': { value: reserved },
    }];

如果键不变,哪个可以正常工作。

StackedBarChart渲染:

stackedBarChart(counts, triggers) {
    return (
      <View>
        <StackedBarChart
          style={{ height: 10 }}
          colors={this.triggerColors()}
          contentInset={{ top: 30, bottom: 30 }}
          data={this.triggerValues(counts)}
          horizontal={true}
          keys={this.triggerKeys(counts)}
          valueAccessor={({ item, key }) => item[key].value}
        />
      </View>
    );
  }

this.triggerColors

triggerColors() {
  return ['#00c8c6', '#44baf1', '#c7e486', '#efdc6c'];
}

this.triggerKeys

triggerKeys(data) {
  return Object.keys(data);
}

当前实施以构建所需的结构:

triggerValues(data) {
    // The trigger values can be a lot more, so this needs to be dynamic
    const keys = this.triggerKeys(data);
    let manuallySoldValue = '';
    let trailingStopLossValue = '';
    let manualShortValue = '';
    let profitValue = '';
    let manualBuy = '';
    let strategy = '';
    let manualCloseShort = '';
    let available = '';
    let inOpenPositions = '';
    let reserved = '';

    keys.map((key) => {
      switch (key) {
        case 'Manually Sold':
          manuallySoldValue = data[key];
          break;
        case 'Trailing Stop Loss':
          trailingStopLossValue = data[key];
          break;
        case 'Manual Short':
          manualShortValue = data[key];
          break;
        case 'Profit':
          profitValue = data[key];
          break;
        case 'Manual Buy':
          manualBuy = data[key];
          break;
        case 'Strategy':
          strategy = data[key];
          break;
        case 'Manual Close Short':
          manualCloseShort = data[key];
          break;
        case 'available':
          available = data[key];
          break;
        case 'in open positions':
          inOpenPositions = data[key];
          break;
        case 'reserved':
          reserved = data[key];
          break;
        default:
          break;
      }
    });

    return [{
      'Manually Sold': { value: manuallySoldValue },
      'Trailing Stop Loss': { value: trailingStopLossValue },
      'Manual Short': { value: manualShortValue },
      'Profit': { value: profitValue },
      'Manual Buy': { value: manualBuy },
      'Strategy': { value: strategy },
      'Manual Close Short': { value: manualCloseShort },
      'available': { value: available },
      'in open positions': { value: inOpenPositions },
      'reserved': { value: reserved },
    }];
  }

如果仅将按键设置在石头上,那将起作用。因此,我想构建一个数量和键无关紧要的部分。我试图通过迭代在数据对象(下面的示例对象)上构建它,但是我似乎无法获得如下所述的正确结构。

数据对象示例:

{available: "46.09", in open positions: "53.91", reserved: "0.00"}

-

{Manual Buy: 11, Manual Close Short: 7, Strategy: 42}

-

{Trailing Stop Loss: 3, Manual Short: 7, Profit: 46, Manually Sold: 5}

如果您需要更多信息,请询问。

2 个答案:

答案 0 :(得分:2)

您可以使用Object.keys函数来获取对象键的列表,并对其进行迭代以创建所需的结构。请参见下面的示例代码。

const data = {
  "Trailing Stop Loss": 3,
  "Manual Short": 7,
  "Profit": 46,
  "Manually Sold": 5
};

const result = {};

Object.keys(data).forEach((key) => {
  result[key] = {
    value: data[key]
  };
});

console.log(result);

答案 1 :(得分:1)

更新:

您可以data的{​​{3}} reduce并创建具有嵌套结构的另一个对象:

function triggerValues(data) {
  const output = Object.entries(data)
                       .reduce((r, [key, value]) => ({ ...r, [key]: { value } }), {});
    
  return [output]
}

console.log(triggerValues({available: "46.09", "in open positions": "53.91", reserved: "0.00"}))
console.log(triggerValues({"Manual Buy": 11, "Manual Close Short": 7, Strategy: 42}))

您可以创建一组默认键,并使用简单的entriesfor...of循环

function triggerValues(data) {
  const defaultKeys = [
    "Manually Sold",
    "Trailing Stop Loss",
    "Manual Short",
    "Profit",
    "Manual Buy",
    "Strategy",
    "Manual Close Short",
    "available",
    "in open positions",
    "reserved"
  ];

  const output = {};

  for (const key of defaultKeys) {
    if (key in data)
      output[key] = { value: data[key] }
    else
      output[key] = { value: '' }
  }

  return [output]
}

console.log(triggerValues({available: "46.09", "in open positions": "53.91", reserved: "0.00"}))
console.log(triggerValues({"Manual Buy": 11, "Manual Close Short": 7, Strategy: 42}))