JavaScript/lodash data transformation

自闭症网瘾萝莉.ら 提交于 2020-01-05 03:47:10

问题


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>

Expected:

map which has algo_id as key and temp as values. like below and so on.

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

How to add key and values which are not array in the object.?


回答1:


One way to do this (not using lodash) is the following:

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))

But this makes the assumption that you can use the sibling of algo_id as is. Your example seems to show further processing of it that I can't see any rule for.

If your target environments don't support flat, you can replace this:

      .flat()

with this:

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



回答2:


No need to use lodash, you can do this in plain JavaScript for this.

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
        }
    }
}

-->



回答3:


With lodash, after getting the values, flatten the mixed array of arrays and objects, use keyBy to convert back to an object with the algo_id as key, and then map the sub object to omit the algo_id property.

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; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.min.js"></script>


来源:https://stackoverflow.com/questions/55290817/javascript-lodash-data-transformation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!