1

So How to merge values of a Json object like this: {"zz0": "value 1","zz1": "value 2","zz2": "value 3"} into this: {"key":["value 1","value 2","value 3"]} In my javascript form this is the first Json file:

    [
        {
            "fotos": [
                {
                    "foto": "foto 1",
                    "zz0": "first line.",
                    "zz1": "second line.",
                    "zz2": "third line."
                },
                {
                    "foto": "foto 2",
                    "zz0": "first line."
                }
            ]
        }
    ]

And this is the second Json File:

    [
        {
            "fotos": [
                {
                    "foto": ["foto 1"],
                    "tekst": ["first line.", "second line.", "third line."]
                },
                {
                    "foto": ["foto 2"],
                    "tekst": ["first line."]
                }
            ]
        }
    ]

Searched a solution with map and arrow functions but got stuck... Any idea?

7
  • What logical representation are you after? The example you provided isn't logically clear, all keys are different so how do you logically group the keys together? Ex: {"zz0": "value 1","zz1": "value 2","zz2": "value 3"} into this: {"key":["value 1","value 2","value 3"]} Commented Nov 22, 2022 at 20:29
  • The keys 'zz0', 'zz1' and 'zz2' should be replaced by 1 key: 'key' and their values put together as values for 'key' (in the big example: 'tekst') Commented Nov 22, 2022 at 20:39
  • do objects only contain one property foto and many properties zz*? I mean, are there more properties? Commented Nov 22, 2022 at 20:41
  • in 'fotos' each single 'foto' has many lines 'tekst' Commented Nov 22, 2022 at 20:46
  • 1
    @backnext I think my solution IS NOT as performant as the others, you should choose between the others Commented Nov 23, 2022 at 13:41

4 Answers 4

2

const arr = [{ "fotos": [{ "foto": "foto 1", "zz0": "first line.", "zz1": "second line.", "zz2": "third line." }, { "foto": "foto 2", "zz0": "first line." }]}];

const ans = arr.map(({fotos}) => ({fotos: fotos.map(({foto, ...res}) =>({foto: [foto], tekst: Object.values(res)}))}));

console.log(ans);

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

Comments

2

Using Array.map(), Object.keys() anf Array.reduce():

const data = [{
  "fotos": [{
      "foto": "foto 1",
      "zz0": "first line.",
      "zz1": "second line.",
      "zz2": "third line."
    },
    {
      "foto": "foto 2",
      "zz0": "first line."
    }
  ]
}]

const res = data[0].fotos.map(obj => {
  const tekst = Object.keys(obj).reduce((acc,key) => {
    if (key != 'foto') { acc.push(obj[key]) }
    return acc
  }, [])
  
  return { foto: [obj.foto], tekst: tekst }
})

console.log(res)

2 Comments

Works good and very nice. Thanx. Maybe I prefer the solution of Astraf...
@Asraf's solution is my favourite too!
1

The following would do the job:

const dat=[
    {
        "fotos": [
            {
                "foto": "foto 1",
                "zz0": "first line.",
                "zz1": "second line.",
                "zz2": "third line."
            },
            {
                "foto": "foto 2",
                "zz0": "first line."
            }
        ]
    }
];

const res=dat[0].fotos.map(f=>({foto:f.foto,key:Object.entries(f).filter(a=>a[0]!='foto').map(a=>a[1])}));

console.log(res);

1 Comment

apparently I have a choice of several perfectly working and brilliant options here. Thank you.
0

You could merge them like this with reduce

const a = {a: 2, b: 3, c: 4}
Object.keys(a).reduce((acc, key) => {
    acc['key'].push(a[key])
    return acc
}, {'key': []})

this would give you {'key': [2, 3, 4]}

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.