2

I'm struggling to add(convert json to list of jsons) list to json. Here is the example:

config = {
  existing_variable: 'example'
}


addToListing = (field, value, index=0) => {
  config = {
    ...config,
    listing: {   
      ...config.listing,
      [field]: value
    }   
  }
}

addToListing('first', 1, 0)
addToListing('second', 2, 0)
addToListing('first', 3, 1)


console.log(config)

Result:

{
  existing_variable: 'example', 
  listing: 
  { 
    first: 3, 
    second: 2 
  } 
}

Expected result:

{ 
  existing_variable: 'example', 
  listing: 
  [
    {first: 1, second: 2}, 
    {first: 3}
  ] 
}

I've tried different approaches, but what I did wasn't suitable(e.g nested values or complete initialization at beggining).

Could you please help?

2
  • what is a list of JSON? Commented Apr 17, 2020 at 8:35
  • I'm new to JS, you are both right this is object, not JSON. Do you have any idea how to solve this? Commented Apr 17, 2020 at 8:44

2 Answers 2

1

You could add some checks and rewind the array.

const addToListing = (field, value, index=0) => {
    config = {
        ...config,
        listing: Object.assign(
            [],
            (config.listing || []), 
            { [index]: { ...(config.listing?.[index] || []), [field]: value } }
        )    
    }
};

var config = { existing_variable: 'example' };

addToListing('first', 1, 0);
addToListing('second', 2, 0);
addToListing('first', 3, 1);

console.log(config)
.as-console-wrapper { max-height: 100% !important; top: 0; }

Without optional chaining operator ?. but with more default values.

const addToListing = (field, value, index=0) => {
    config = {
        ...config,
        listing: Object.assign(
            [],
            (config.listing || []), 
            { [index]: { ...((config.listing || [])[index] || []), [field]: value } }
        )    
    }
};

var config = { existing_variable: 'example' };

addToListing('first', 1, 0);
addToListing('second', 2, 0);
addToListing('first', 3, 1);

console.log(config)
.as-console-wrapper { max-height: 100% !important; top: 0; }

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

1 Comment

It works, but unfortunately I cannot use optional chaining :/
0

I am proposing an alternative approach using Array.prototype.splice().

let config = {
  existing_variable: 'example'
}


const addToListing = (field, value, index = 0) => {
	// If listing property not exists then init it with an empty array
	config.listing || (config.listing = [])
	
	config.listing.splice(index, 1, (config.listing[index] ? {...config.listing[index], [field]: value} : {[field]: value}));
}

addToListing('first', 1, 0)
addToListing('second', 2, 0)
addToListing('first', 3, 1)


console.log(config);
.as-console-wrapper {min-height: 100% !important; top: 0;}

2 Comments

Actually there should be 2 objects(first two values in first object, and third value in second object) in this array instead of 3.
Sorry for the inconvinience. I've updated my answer. Please have a look.

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.