Skip to main content
Typo
Source Link
Mohrn
  • 400
  • 2
  • 8

Some slight improvements:

  • You could do without the result array (less state 🙌).
  • I think using vanilla object destructuring is more clear than the lowdash reduce method which is confusing to me (unless I still don't understand it and my code actually is not equivalent to yours 😅).
const allNodes = [
];

const rootNode = {
};

function initializeNodeMapper(nodes) {
  return function mapNodes(node) {
    const propertyIds = node.properties.map(({ id }) => id);
    const currentNode = nodes.find(({ id }) => id === node.id);
    const nodeWithFilteredProperties = {
      ...currentNode,
      properties: currentNode.properties.filter(({ id }) =>
        propertyIds.includes(id)
      )
    };

    return node.children
      ? [nodeWithFilteredProperties].concat(node.children.map(mapNodes))
      : [nodeWithFilteredProperties];
  };
}

const mappedData = initializeNodeMapper(allNodes)(rootNode);

Update:

Are you looking for something like this?

const nodes = [
];

const rootNode = {
};

const pipe = (...fns) => x => fns.reduce((v, f) => f(v), x);

const getFlattenedNodes = node =>
  node.children
    ? [node].concat(
        node.children.reduce(
          (acc, val) => acc.concat(getFlattenedNodes(val)),
          []
        )
      )
    : [node];

const getPropertyIds = node => node.properties.map(({ id }) => id);

const getNodeWithProperties = nodesWithProperties => node =>
  nodesWithProperties.find(({ id }) => id === node.id);

const getNodesWithFilteredProperties = propertyIdsAndNodesToBeFiltered =>
  propertyIdsAndNodesToBeFiltered.map(([propertyIds, nodeWithProperties]) => ({
    ...nodeWithProperties,
    properties: nodeWithProperties.properties.filter(({ id }) =>
      propertyIds.includes(id)
    )
  }));

const getPropertyIdsAndNodes = nodesWithProperties => flattenNodes =>
  flattenNodes.map(node => [
    getPropertyIds(node),
    getNodeWithProperties(nodesWithProperties)(node)
  ]);

const mappedData = pipe(
  getFlattenedNodes,
  getPropertyIdsAndNodes(nodes),
  getNodesWithFilteredProperties
)(rootNode);

Some slight improvements:

  • You could do without the result array (less state 🙌).
  • I think using vanilla object destructuring is more clear than the lowdash reduce method which is confusing to me (unless I still don't understand it and my code actually is not equivalent to yours 😅).
const allNodes = [
];

const rootNode = {
};

function initializeNodeMapper(nodes) {
  return function mapNodes(node) {
    const propertyIds = node.properties.map(({ id }) => id);
    const currentNode = nodes.find(({ id }) => id === node.id);
    const nodeWithFilteredProperties = {
      ...currentNode,
      properties: currentNode.properties.filter(({ id }) =>
        propertyIds.includes(id)
      )
    };

    return node.children
      ? [nodeWithFilteredProperties].concat(node.children.map(mapNodes))
      : [nodeWithFilteredProperties];
  };
}

const mappedData = initializeNodeMapper(allNodes)(rootNode);

Some slight improvements:

  • You could do without the result array (less state 🙌).
  • I think using vanilla object destructuring is more clear than the lowdash reduce method which is confusing to me (unless I still don't understand it and my code actually is not equivalent to yours 😅).
const allNodes = [
];

const rootNode = {
};

function initializeNodeMapper(nodes) {
  return function mapNodes(node) {
    const propertyIds = node.properties.map(({ id }) => id);
    const currentNode = nodes.find(({ id }) => id === node.id);
    const nodeWithFilteredProperties = {
      ...currentNode,
      properties: currentNode.properties.filter(({ id }) =>
        propertyIds.includes(id)
      )
    };

    return node.children
      ? [nodeWithFilteredProperties].concat(node.children.map(mapNodes))
      : [nodeWithFilteredProperties];
  };
}

const mappedData = initializeNodeMapper(allNodes)(rootNode);

Update:

Are you looking for something like this?

const nodes = [
];

const rootNode = {
};

const pipe = (...fns) => x => fns.reduce((v, f) => f(v), x);

const getFlattenedNodes = node =>
  node.children
    ? [node].concat(
        node.children.reduce(
          (acc, val) => acc.concat(getFlattenedNodes(val)),
          []
        )
      )
    : [node];

const getPropertyIds = node => node.properties.map(({ id }) => id);

const getNodeWithProperties = nodesWithProperties => node =>
  nodesWithProperties.find(({ id }) => id === node.id);

const getNodesWithFilteredProperties = propertyIdsAndNodesToBeFiltered =>
  propertyIdsAndNodesToBeFiltered.map(([propertyIds, nodeWithProperties]) => ({
    ...nodeWithProperties,
    properties: nodeWithProperties.properties.filter(({ id }) =>
      propertyIds.includes(id)
    )
  }));

const getPropertyIdsAndNodes = nodesWithProperties => flattenNodes =>
  flattenNodes.map(node => [
    getPropertyIds(node),
    getNodeWithProperties(nodesWithProperties)(node)
  ]);

const mappedData = pipe(
  getFlattenedNodes,
  getPropertyIdsAndNodes(nodes),
  getNodesWithFilteredProperties
)(rootNode);
Source Link
Mohrn
  • 400
  • 2
  • 8

Some slight improvements:

  • You could do without the result array (less state 🙌).
  • I think using vanilla object destructuring is more clear than the lowdash reduce method which is confusing to me (unless I still don't understand it and my code actually is not equivalent to yours 😅).
const allNodes = [
];

const rootNode = {
};

function initializeNodeMapper(nodes) {
  return function mapNodes(node) {
    const propertyIds = node.properties.map(({ id }) => id);
    const currentNode = nodes.find(({ id }) => id === node.id);
    const nodeWithFilteredProperties = {
      ...currentNode,
      properties: currentNode.properties.filter(({ id }) =>
        propertyIds.includes(id)
      )
    };

    return node.children
      ? [nodeWithFilteredProperties].concat(node.children.map(mapNodes))
      : [nodeWithFilteredProperties];
  };
}

const mappedData = initializeNodeMapper(allNodes)(rootNode);