3

i have an array like this

var data = [
    {
        name: "Movies", info: "category_name",
        content: [
            { name: "Interstellar", info: "category_data" },
            { name: "Dark Knight", info: "category_data" },
        ]
    },
    {
        name: "Music", info: "category_name",
        content: [
            { name: "Adams", info: "category_data" },
            { name: "Nirvana", info: "category_data" },
        ]
    },
    {
        name: "Places", info: "category_name",
        content: [
            { name: "Jordan", info: "category_data" },
            { name: "Punjab", info: "category_data" },
        ]
    },
]

and a want to transform it into like this

var transformedArray= [
  { name: "Movies", info: "category_name" },
  { name: "Interstellar", info: "category_data" },
  { name: "Dark Knight", info: "category_data" },
  { name: "Music", info: "category_name" },
  { name: "Adams", info: "category_data" },
  { name: "Nirvana", info: "category_data" },
  { name: "Places", info: "category_name" },
  { name: "Jordan", info: "category_data" },
  { name: "Punjab", info: "category_data" },
]

i dont know what is the keyword suitable for this case , i have tried mapping it into new array but it's not same like i expected

var newArr = []

var manipulate = data.map(item => {
    return (
        newArr.push(item),
        new1.map(items => {
            return (
                new1.push(items)
            )
        })
    )
})

then how to manipulate "data" into "transformedArray"

1
  • Would you mind explaining your code? It doesn't make that much sense (at least for me) Commented Oct 12, 2019 at 16:08

2 Answers 2

6

Use Array.prototype.flatMap(). If your browser or version of Node.js does not yet natively support this function, you can include a simple polyfill below, based on the specification:

if (!Array.prototype.flatMap) {
  Object.defineProperty(Array.prototype, 'flatMap', {
    configurable: true,
    writable: true,
    value: function flatMap (callback, thisArg = undefined) {
      return this.reduce((array, ...args) => {
        const element = callback.apply(thisArg, args);

        if (Array.isArray(element)) array.push(...element);
        else array.push(element);

        return array;
      }, []);
    }
  });
}

const data = [{name:'Movies',info:'category_name',content:[{name:'Interstellar',info:'category_data'},{name:'Dark Knight',info:'category_data'}]},{name:'Music',info:'category_name',content:[{name:'Adams',info:'category_data'},{name:'Nirvana',info:'category_data'}]},{name:'Places',info:'category_name',content:[{name:'Jordan',info:'category_data'},{name:'Punjab',info:'category_data'}]}];
const transformedArray = data.flatMap(({ content, ...o }) => [o, ...content]);

console.log(transformedArray);

Sign up to request clarification or add additional context in comments.

7 Comments

it is returning "TypeError: data.flatMap is not a function"
@KhadamIkhwanus it's a relatively new function in the specification, you might need a polyfill depending on what browser you're using. I'll update my answer to include that in just a second.
i'm do it in my terminal and using node js version 10.16.3
thankyou somuch brother you made my day ,but why i can't use flatmap directly ? is it the my node js problem or anything else ?
@KhadamIkhwanus your version of Node.js is not problematic, it just doesn't support ES2019 specification. I believe flatMap support isn't available until Node v12.x.
|
4

You could do this using reduce method and spread syntax ....

var data = [{"name":"Movies","info":"category_name","content":[{"name":"Interstellar","info":"category_data"},{"name":"Dark Knight","info":"category_data"}]},{"name":"Music","info":"category_name","content":[{"name":"Adams","info":"category_data"},{"name":"Nirvana","info":"category_data"}]},{"name":"Places","info":"category_name","content":[{"name":"Jordan","info":"category_data"},{"name":"Punjab","info":"category_data"}]}]

const result = data.reduce((r, {content, ...rest}) => {
  r.push({...rest}, ...content)
  return r;
}, []);

console.log(result);

You can also use concat and reduce instead of spread syntax.

var data = [{"name":"Movies","info":"category_name","content":[{"name":"Interstellar","info":"category_data"},{"name":"Dark Knight","info":"category_data"}]},{"name":"Music","info":"category_name","content":[{"name":"Adams","info":"category_data"},{"name":"Nirvana","info":"category_data"}]},{"name":"Places","info":"category_name","content":[{"name":"Jordan","info":"category_data"},{"name":"Punjab","info":"category_data"}]}]
const result = data.reduce((r, {content, ...rest}) => r.concat(rest, content), []);
console.log(result);

4 Comments

this is precisely what .flatMap does
flatmap cannot be used in my console , i think this might work ,but can you explain where r and rest came from
r is the accumulator parameter and next parameter is current object in iteration so we use parameter destructuring to get the content property and also rest parameter to get the rest of the properties.
@Khadam Ikhwanus For better understanding you could also do this jsfiddle.net/t28uy79g/1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.