0

The task is to slice a nested array by data property. I have the following array structure:

  const mockData = [
    {
      text: 'Text1',
      data: [
        { field: '1' },
        { field: '2' },
        { field: '3' },
        { field: '4' },
        { field: '5' },
        { field: '6' }
      ]
    },
    {
      text: 'Text2',
      data: [{ field: '1' }, { field: '2' }, { field: '3' }, { field: '4' }]
    }
  ];

Here's the method I use:

const sliceArray = mockData => mockData.map(d => ({...d, data: d.data.slice(0, 3)}))

It goes through all nested objects and slice array by data property, but how can I do it for a specific nested object instead of all of them? I'd like to use text property as a key.

So, if I pass Text1 to a method - data property in the first object only should be sliced and the output should be:

 const mockData = [
    {
      text: 'Text1',
      data: [{ field: '1' }, { field: '2' }, { field: '3' }]
    },
    {
      text: 'Text2',
      data: [{ field: '1' }, { field: '2' }, { field: '3' }, { field: '4' }]
    }
  ];

If I pass 'Text2':

 const mockData = [
    {
      text: 'Text1',
       data: [
        { field: '1' },
        { field: '2' },
        { field: '3' },
        { field: '4' },
        { field: '5' },
        { field: '6' }
      ]
    },
    {
      text: 'Text2',
      data: [{ field: '1' }, { field: '2' }, { field: '3' }]
    }
  ];

What can be the solution? Thank you!

2
  • how will the desired output look like? Commented Apr 1, 2020 at 16:30
  • There's an output in the question. So, if I pass Text1 only in object with a 'text' = Text1, data array should be sliced. Other objects should remain. Commented Apr 1, 2020 at 16:33

3 Answers 3

3

You can try to add a condition like this:

const sliceArray = (mockData, text) => 
                     mockData.map(d => d.text === text 
                        ? {...d, data: d.data.slice(0, 3)} 
                        : d)
Sign up to request clarification or add additional context in comments.

Comments

2

You can just add another parameter to your function and check if the text matches that parameter.

 const mockData = [{"text":"Text1","data":[{"field":"1"},{"field":"2"},{"field":"3"},{"field":"4"},{"field":"5"},{"field":"6"}]},{"text":"Text2","data":[{"field":"1"},{"field":"2"},{"field":"3"},{"field":"4"}]}]

const sliceArray = (data, target, len = 3) => 
  data.map(({ text, data, ...rest}) => ({
    data: text == target ? data.slice(0, len) : data,
    text,
    ...rest
  }))

console.log(sliceArray(mockData, 'Text1'))

You could also pass an array of text values that you want to match and use includes method for checking.

 const mockData = [{"text":"Text1","data":[{"field":"1"},{"field":"2"},{"field":"3"},{"field":"4"},{"field":"5"},{"field":"6"}]},{"text":"Text2","data":[{"field":"1"},{"field":"2"},{"field":"3"},{"field":"4"}]}, {"text":"Text3","data":[{"field":"1"},{"field":"2"},{"field":"3"},{"field":"4"},{"field":"5"},{"field":"6"}]}]

const sliceArray = (data, targets, len = 3) => 
  data.map(({ text, data, ...rest}) => ({
    data: targets.includes(text) ? data.slice(0, len) : data,
    text,
    ...rest
  }))

console.log(sliceArray(mockData, ['Text1', 'Text3']))

Comments

1

If you dont wanna to modify existing data, Since you are mocking. use reduce

const mockData = [
  {
    text: "Text1",
    data: [
      { field: "1" },
      { field: "2" },
      { field: "3" },
      { field: "4" },
      { field: "5" },
      { field: "6" }
    ]
  },
  {
    text: "Text2",
    data: [{ field: "1" }, { field: "2" }, { field: "3" }, { field: "4" }]
  }
];

function update(data, text, count = 3) {
  return data.reduce((arr, item) => {
    let updatedItem = { ...item };
    if (item.text === text) {
      updatedItem.data = (updatedItem.data || []).slice(0, count);
    }
    arr.push(updatedItem);
    return arr;
  }, []);
}

console.log("%j", update(mockData, "Text1"));
.as-console-row {color: blue!important}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.