JavaScript Object array not fetching when the value string has spaces

javascript object property with spaces
accessing json object keys having spaces
javascript object get value by key
remove spaces in json keys
how to access object key with space
javascript match object value
json object key with spaces
typescript object property with space

This is the jsFiddle of my codeNow I'm trying to make a cart item increment when I press the plus button and also decrement too. It works fine for single text item name like.."Chicken" but It doesn't work when the Item name has space in it like..."chicken and chips". This is the script code responsible for adding and substracting items already in the cart.

  // Add to cart
  obj.addItemToCart = function(name, price, count) {
    for(var item in cart) {
      if(cart[item].name === name) {
        cart[item].count ++;
        saveCart();
        return;
      }
    }
    var item = new Item(name, price, count);
    cart.push(item);
    saveCart();
  }

  // Remove item from cart
  obj.removeItemFromCart = function(name) {
      for(var item in cart) {
        if(cart[item].name === name) {
          cart[item].count --;
          if(cart[item].count === 0) {
            cart.splice(item, 1);
          }
          break;
        }
    }
    saveCart();
  }

https://jsfiddle.net/wbotk4ue/

Replacing the "" with template strings `` solves the issue. The problem was with spaces in data-name

How to access an object having spaces in the object's key using , Recommended Posts: How to access first value of an object using JavaScript ? How to remove spaces from a string using JavaScript ? Replacing spaces with  Since strings are objects in javascript, why shouldn't the 'in' keyword work for them? Not quite. There is a difference between a primitive string value and a String instance


SOLVED! I solved it by adding single quotes outside the double quotes containing the array value i.e data-name='" + cartArray[i].name + "' to the increment button and decrement button

function displayCart() {
  var cartArray = shoppingCart.listCart();
  var shipping = 120;
  var output = "";
  for(var i in cartArray) {
    output += "<tr>"
      + "<td>" + cartArray[i].name + "</td>" 
      + "<td>(" + cartArray[i].price + ")</td>"
      + "<td><div class='input-group'><button class='minus-item input-group-addon btn btn-primary' data-name='" + cartArray[i].name + "'>-</button>"
      + "<input type='number' class='item-count form-control' data-name='" + cartArray[i].name + "' value='" + cartArray[i].count + "'>"
      + "<button class='plus-item btn btn-primary input-group-addon' data-name='" + cartArray[i].name + "'>+</button></div></td>"
      + "<td><button class='delete-item btn btn-danger' data-name='" + cartArray[i].name + "'>X</button></td>"
      + " = " 
      + "<td>" + cartArray[i].total + "</td>" 
      +  "</tr>";
  }
  $('.show-cart').html(output);
  $('.PandD').html(shipping)
  $('.total-cart').html(shoppingCart.totalCart() + shipping);
  $('.total-count').html(shoppingCart.totalCount());
}

How to remove spaces from a string using JavaScript, It contains well written, well thought and well explained computer science and The space character (” “) is specified in this parameter to separate the string This method is used on the returned array and no separator (“”) is used to join the How to access an object having spaces in the object's key using JavaScript ? In case the value is null or empty, then all properties of an object will be appended to the string. Space: Again, it is an optional parameter for JSON.stringify JavaScript function. To control the spacing in the string generated eventually, you will be using this parameter. As for the value, you can either pass a string or number.


This solution could be fine

value.replace(/ /g, '%25');

Python RegEx, how to use HTML, CSS, JavaScript, SQL, PHP, Python, Bootstrap, Java and XML. RegEx can be used to check if a string contains the specified search pattern. \S, Returns a match where the string DOES NOT contain a white space character is no match, the value None will be returned, instead of the Match Object. Search should return the object that match the search should return undefined becuase non of the objects in the array have that value should return undefined becuase our array of objects dont have ids 3 passing (12ms) Old answer - removed due to bad practices


print_r - Manual, If given an array, values will be presented in a format that shows keys and When the return parameter is TRUE , this function will return a string. make sure we only match stuff with 4 preceding spaces (stuff for this array and not a nested one) print_r(), just like var_dump() does NOT cast an object, not even if it has a  Array To String With Spaces Java. Array To String With Spaces Java


Data Structures: Objects and Arrays :: Eloquent JavaScript, The notation for getting at the elements inside an array also uses square brackets​. Both value.x and value[x] access a property on value —but not necessarily the Both string and array values contain, in addition to the length property, To find out what properties an object has, you can use the Object.keys function. The answer of Pardeep Jain can be useful for static data, but what if we have an array in JSON? For example, we have i values and get the value of id field. alert(obj[i].id); //works! But what if we need key with spaces? In this case, the following construction can help (without point between [] blocks): alert(obj[i]["No. of interfaces


Beginning JavaScript, CONTENTS Using JavaScript Objects Creating an Object Using an Object's JavaScript's Native Object Types String Objects The length Property Finding a String Trailing Whitespace—The trim() Method Array Objects Finding Out How Many Creating a Date Object Getting Date Values Setting Date Values Calculations  Sorry about not mentioning that someObject is not initialized yet. As for the reason, someObject is fetch via web service. And I want to have an array of header which consist of part1name, part2qty, etc. So that I could just loop through the header array and get the value I wanted based on part1name value as the 'key'/path to someObject.