Need help with getting object property values

const carObj = {type: "Fiat", model: "500", color: "white"};

function addYourCar(object, property, value) {
    if (value != "" && property === object.hasOwnProperty(property)) {
        object[property].push(value);
    } else if (value != "" && property != object.hasOwnProperty(property)) {
        object.push(property);
        object[property].push(value);
    } else {
        return "Fatal error!";
    }
};
addYourCar(carObj, type, "Audi");
document.write(carObj.type);

Hy everybody!
This is my code. But the function() not working. I stuck… Please help me!
I wanna:
if (value not empty string && object has property) {give value for property}
else if ( value not empty string && object not has property) {give new property and value too}

My english is poor. Sorry! Thanks

Thank you your answer.
I have an object{carObj} and I want to build a function addYourCar(). It´s similar to FCC, Javascript, Record collection. When somebody give property and value: type, Audi or color, black or new property wheel, 17…then search tought carObj && make new property with value || refresh property with new value.
Thank you very much!

The line above would only work if object[property] is an array.

The line above would only work if object is an array and it is not an array.

Thank you very much!

function addYourCar(object, property, value) {
    if (object.hasOwnProperty(property) && value != "") {
        object[property] = value;
    } else if (object.hasOwnProperty(property) != property && value != "") {
        let xProp = property;
        object[xProp] = value;
    } else if (value === "" || property === "") {
        return alert("You need add some property and value!");
    }
}
addYourCar(carObj, "ps", "65");
document.write(carObj.ps);

/* Now working! */

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.