0

The following code loops through a JavaScript object and collects only the properties that are arrays:

const building = this.building
let panoramaList = []
for (let key in building) {
  const panoramas = building[key]
  if (Array.isArray(panoramas)) {
    panoramaList.push({ [key]: panoramas })
  }
}
console.log(panoramaList)

In other words, it takes this:

{
  name: '',
  description: ''.
  livingroom: Array[0],
  study: Array[1],
  bedroom: Array[0]
}

and turns it into this:

[
  { livingroom: Array[0] },
  { study: Array[1] },
  { bedroom: Array[0] }
]

However, what I need to produce is this:

{
  livingroom: Array[0],
  study: Array[1],
  bedroom: Array[0]
}

How to accomplish that?

3
  • you might want to read this stackoverflow.com/questions/4215737/convert-array-to-object Commented Mar 31, 2016 at 9:02
  • 1
    Why not just delete the unwanted properties? Commented Mar 31, 2016 at 9:25
  • @RobG Could you provide an example code for that? Commented Mar 31, 2016 at 10:07

5 Answers 5

1

Change this :

const building = this.building
let panoramaList = []
for (let key in building) {
  const panoramas = building[key]
  if (Array.isArray(panoramas)) {
    panoramaList.push({ [key]: panoramas })
  }
}
console.log(panoramaList)

to this :

const building = this.building
let panoramaList = {}
for (let key in building) {
  const panoramas = building[key]
  if (Array.isArray(panoramas)) {
    panoramaList[key]=panoramas
  }
}
console.log(panoramaList)
Sign up to request clarification or add additional context in comments.

Comments

1

Use Object.keys and try something like this:

var input = {} //...your input array
var keys = Object.keys(input);
var result = {};

keys.forEach(function (key) {
    if (Array.isArray(input[key])) {
        result[key] = input[key];
    }
});

Comments

1

try this

var output = Object.keys(building).map(function(val){ return { val : building[val] } });

For the final output

var panoramaList = {}
Object.keys(building).forEach(function(val){ 
  if ( Array.isArray(building[val] )
  {
    panoramaList[val] = building[val];
  }
});

2 Comments

Thanks but I get key is not defined in ` building[key]`.
output should be an object and only arrays hould be inserted as property, not as own object in an array.
0

Make sure to define panoramaList as an object.

This works

var arrays = {
  name: '',
  description: '',
  livingroom: ['1','www'],
  study: ['2','sss'],
  bedroom: ['3','aaa'],
  Kitchen: ['4','bbb'],
}    


const building = arrays
let panoramaList = {};
for (let key in building) {
  const panoramas = building[key]

  if (Array.isArray(panoramas)) {
      panoramaList[key] = panoramas;
  }
}
console.log(panoramaList);

https://jsbin.com/paqebupiva/1/edit?js,console,output

Comments

0

Rather than building a new object, you might just need to delete the unwanted properties from the object that you have:

var data = {
  name: '',
  description: '',
  livingroom: [],
  study: [1],
  bedroom: [0]
};

Object.keys(data).forEach(function(key) {
  if (!Array.isArray(data[key])) delete data[key];
})

document.write(JSON.stringify(data));

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.