0

Have an array which contains a no of json .

[{linkValue:"value1"},{linkValue:"value2"},{linkValue:"value3"},{linkValue:"value4"},{linkValue:"value5"}]

Note that each Json have same key . I want to convert this array into a single json like

{linkValue1:"value1",linkValue2:"value2",linkValue3:"value3",linkValue4:"value4",linkValue5:"value5"}

one thing i also need to know . my array is also inside a json how i get this arry from that json ?

My initial json is

{name:"value",age:"value",linkValue:[[{linkValue:"value1"},{linkValue:"value2"},{linkValue:"value3"},{linkValue:"value4"},{linkValue:"value5"}]
]}

I'm expcting my final json look like :

{name:"value",age:"value",linkValue1:"value1",linkValue2:"value2",linkValue3:"value3",linkValue4:"value4",linkValue5:"value5"}

can anyone please help me

2
  • i want to convert it into a json Commented Jun 15, 2017 at 6:44
  • JSON is a data exchanging format, this has nothing to do with JSON. Commented Jun 15, 2017 at 6:45

5 Answers 5

1

Use Array.forEach and add properties to an empty object:

let source = {name:"value",age:"value",linkValue:[[{linkValue:"value1"},{linkValue:"value2"},{linkValue:"value3"},{linkValue:"value4"},{linkValue:"value5"}]]};

// Copy the array in a variable
let yourArray = source.linkValue[0];

// Delete the original array in the source object 
delete source.linkValue;

yourArray.forEach((item, index) => {
    source["linkValue" + (index + 1)] = item.linkValue
});

console.log(source); // Should have what you want
Sign up to request clarification or add additional context in comments.

5 Comments

Insted forEach you could use reduce.
@PiotrPasieka why don't you provide it as an answer then? :-)
one thing i also need to know . i forget it to include in ma question . my array is inside an json object how i get that array .
@Ajmalsha That depends entirely on your JSON structure.
@PiotrPasieka Its working can you please give a suggestion for how i get my final result
0

Using reduce API,

let targetObj = srcArr.reduce((accumulator, value, index)=>{
   accumulator['linkValue'+(index+1)] = value.linkValue;
   return accumulator;
}, {});

6 Comments

Check @PiotrPasieka's answer. Don't you think it's a bit.. the same?
what is the difference between this two
i get the value like this {linkValue1: undefined, linkValue2: undefined}
I just cross checked my solution and it is returning me correct object, { linkValue1: 'value1', linkValue2: 'value2', linkValue3: 'value3', linkValue4: 'value4', linkValue5: 'value5' }
Ok its working fine . can you please give a suggestion for how i get my final result
|
0
[{linkValue:"value1"},{linkValue:"value2"},{linkValue:"value3"},{linkValue:"value4"},{linkValue:"value5"}]

This is javascript Array contains multiple javascript object.

{linkValue1:"value1",linkValue2:"value2",linkValue3:"value3",linkValue4:"value4",linkValue5:"value5"}

If you need structure like this,Then define a single javascript object and add linkvalue1,linkvalue2 etc. as a member of that object and then add it to javascript Array.

Comments

0

Give this a try.

myObj.linkValue = myObj.linkValue.map((obj, index) => ({ [`linkValue${index + 1}`]: obj.linkValue }))

Comments

0

Solution with reduce

// HELPER FUNCTIONS

// pick properties from object with default value
function pick (props, sourceObj, defaultValue) {
  return props.reduce(
    (obj, prop) =>
      Object.assign(obj, { [prop]: sourceObj[prop] || defaultValue }),
    {}
  )
}

// get property value or default
function propOr (propName, obj, defaultValue) {
  return obj[propName] || defaultValue
}

// flatten nested array
function flattern (nestedArray) {
  return nestedArray.reduce((flat, innerArray) => flat.concat(innerArray), [])
}

// LINKS BUILDER based on REDUCE
function buildLinks (linksArray) {
  return linksArray.reduce((accumulator, item, index) => {
    accumulator['linkValue' + (index + 1)] = item.linkValue
    return accumulator
  }, {})
}


// TRANSFORMATION FUNCTION - takes json and produce required output
function transform(json) {
  return Object.assign(
    {}, 
    pick(['name', 'age'], json, null), 
    buildLinks(flattern(propOr('linkValue', json, [])))
  )
}

// EXECUTION
const result = transform(source)

PS> You can use libraries like Lodash or Ramda and replace helper functions defined by me with ones from library

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.