JavaScript / lodash数据转换

时间:2019-03-21 23:41:42

标签: javascript lodash

var original = {
  "8": [{
      "temp": {
        "a": 1
      },
      "algo_id": 1
    },
    {
      "temp": {
        "a": 2
      },
      "algo_id": 101
    }
  ],
  "13": {
    "temp": {
      "temp1": [1, 2]
    },
    "algo_id": 2
  }
};

const values = _.values(original);
const temp = _.map(values, (v) => {
  if (_.isArray(v)) {
    return _.mapValues(_.keyBy(v, 'algo_id'), a => _.pick(a, 'temp'));
  }
});

console.log(temp);
.as-console-wrapper { top: 0; max-height: 100% !important; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.min.js"></script>

预期:

地图,其中algo_id为键,而temp为值。 像下面这样等等。

      {
    "1": {
        "temp": {
            "a": 1
        }
    },
    "101": {
        "temp": {
            "a": 2
        }
    },
    "2": {
        "temp": {
            "temp1": [1, 2]
        }
    }
}

如何添加对象中非数组的键和值?

3 个答案:

答案 0 :(得分:1)

执行此操作的一种方法(不使用lodash)如下:

const transform = (original) => Object.values(original)
  .flat()
  .reduce((all, {algo_id, ...rest}) => ({...all, [algo_id]: rest}), {})

const original ={"13": {"algo_id": 2, "temp": {"temp1": [1, 2]}}, "8": [{"algo_id": 1, "temp": {"a": 1}}, {"algo_id": 101, "temp": {"a": 2}}]}

console.log(transform(original))

但这假设您可以按原样使用algo_id的同级。您的示例似乎显示了它的进一步处理,我看不出有任何规定。

如果目标环境为don't support flat,则可以替换为:

      .flat()

与此:

      .reduce((a, b) => a.concat(b), [])

答案 1 :(得分:1)

无需使用lodash,您可以为此使用普通的JavaScript。

let original = {
  "8": [{
      "temp": {
        "a": 1
      },
      "algo_id": 1
    },
    {
      "temp": {
        "a": 2
      },
      "algo_id": 101
    }
  ],
  "13": {
    "temp": {
      "temp1": [1, 2]
    },
    "algo_id": 2
  }
};

console.log(convert(original, 'algo_id'));

function convert(data, key) {
  let process = function(value, key, result) {
    result[value[key]] = value;
    delete result[value[key]][key]; // Remove the `algo_id` key
  };
  return Object.keys(data).reduce((result, k, i) => {
    if (Array.isArray(data[k])) {
      data[k].forEach(val => process(val, key, result));
    } else {
      process(data[k], key, result);
    }
    return result;
  }, {});
}
.as-console-wrapper { top: 0; max-height: 100% !important; }
<!--

{
    "1": {
        "temp": {
            "a": 1
        }
    }
}

-->

答案 2 :(得分:0)

使用lodash,在获取值后,将数组和对象的混合数组展平,使用keyBy将algo_id作为键转换回对象,然后映射子对象以忽略{{1 }}属性。

algo_id
const { flow, partialRight: pr, values, flatten, keyBy, mapValues, omit } = _

const fn = flow(
  values, // convert to a mixed array of objects and arrays
  flatten, // flatten to an array of objects
  pr(keyBy, 'algo_id'), // convert to object with algo_id as key
  pr(mapValues, pr(omit, 'algo_id')), // remove algo_id from the sub objects
);

const original = {"8":[{"temp":{"a":1},"algo_id":1},{"temp":{"a":2},"algo_id":101}],"13":{"temp":{"temp1":[1,2]},"algo_id":2}};

const result = fn(original);

console.log(result);
.as-console-wrapper { top: 0; max-height: 100% !important; }