1

I am having a newbie question and I have tried to read the manuals over and over and cannot figure it out.

so I have this code:

export function editSerier(data, products) {
    return (dispatch) => {

        const filteredProducts = Object.assign(
            ...Object.keys(products)
                .filter(key => products[key].Artikelgrupp === data.Artikelgrupp)
                .map(k => ({ 
                    [k]: products[k]:{
                    Beskrivning: data.Beskrivning,
                    kategori: data.kategori,
                    status: data.status,
                    synas: data.synas,
                    tillverkare: data.tillverkare,
                    titel: data.titel}
                })

            })



        console.log(filteredProducts)
    }
}

Where I want to filter the incoming object products by "Artikelgrupp" and then modify the existent properties of the remaining products with properties from "data".

However this code does not let me run it. Does someone have any idea?

UPDATE: just solved it by merging both objects

const filteredProducts = Object.assign(
            ...Object.keys(products)
                .filter(key => products[key].Artikelgrupp === data.Artikelgrupp)
                .map(k => ({ 
                    [k]: {...products[k], ...data}
                }))
        )

2 Answers 2

1

You have invalid JavaScript. If you want a nested object, you need { something: { } } and if you want to use a computed property name, you need to surround it with [].

So, this will work

export function editSerier(data, products) {
  return dispatch => {
    const filteredProducts = Object.assign(
      ...Object.keys(products)
        .filter(key => products[key].Artikelgrupp === data.Artikelgrupp)
        .map(k => ({
          [k]: {
            [products[k]]: {
              Beskrivning: data.Beskrivning,
              kategori: data.kategori,
              status: data.status,
              synas: data.synas,
              tillverkare: data.tillverkare,
              titel: data.titel
            }
          }
        }))
    );

    console.log(filteredProducts);
  };
}

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

3 Comments

It works but not the way I want. "Data" object is to be merged into products and not be nested as a nested object
just solved it but thanks. Your solution, made me realise where the mistake was.
@OblicionA can you post your solution?
0

If I understand correctly, you are wanting to obtain a single object which:

  • excludes all value objects of products where a Artikelgrupp field does not match data.Artikelgrupp and,
  • the specific fields Beskrivning, kategori, etc, from your data object are merged/copied into the product values of the resulting object

One solution to this would be as

/* Extract entry key/value pairs from products object */
Object.entries(products)
/* Reduce entry pairs to required object shape */
.reduce((result, [key, value]) => {

  /* Substitute for prior filter step. Avoid overhead of array copy
  between prior filter and this reduction. */
  if(value.Artikelgrupp !== data.Artikelgrupp) {
    return result;
  }

  /* Update result object, "upserting" fields of data object into 
  existing product value, for this reduce iteration */
  return {
    ...result,
    [ key ] : {
        ...value,
        Beskrivning: data.Beskrivning,
        kategori: data.kategori,
        status: data.status,
        synas: data.synas,
        tillverkare: data.tillverkare,
        titel: data.titel     
    }
  };

}, {})

Comments

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.