How to get the object key it has the value

Şehir Rehberi

I have a filter component which user can choose any data to filter so I store this data in state.when I want to create a params for query some of the field not choosen by user I only wanna get the one which has value here is the code ;

function createParams(params = {}) {
    let result = "?";
    for (let key in params) {
        result += `${key}=${params[key]}&`;
    }
    return result;
}

export async function callApi(params) {
    const parameters = createParams(params);
    try {
        const response = await fetch(URL+ parameters);
        const res = await response.json();
        return res;
    } catch (error) {
        console.error(error)
        throw error;
    }
}

export const requestProperties = (params) => callApi(params);

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

I only want to get the type and model because it has been choosen by user to filter. I dont wanna include the colour

Thank you..:)

gorak

You can take entries and then filter out the records.

var requestedParams = {type:"Fiat", model:"500", color:""};
var result = Object.fromEntries(Object.entries(requestedParams).filter(([k,v])=>v));
console.log(result);

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to get a value of Object which possible has a undefined key?

how to get object by key and value

Get the value of an object key which has empty value

How can I get a key value from an array that has object inside

How to get key that has max value inside a nested object in MongoDB collection?

Postman - Failing to get value from an object that has `:` in the property key

Get object value that has numeric key in react native

How to get a object's value by a key list

How to get a key in a JavaScript object by its value?

How to get a key/value pair for an object

How to get key and value of object in javascript?

How to get the object key and value into an array in JS?

How to get the key and value from an object in typescript

JS - How to get value from object by key

How to get the value of an object in array by key in Typescript

How to get a value in a JavaScript object by key

How to get key and value of Firestore mapped object

Object get the value by a key

If the JavaScript object has same key with different value, Get only the last key value of the duplicate keys

How to Filter an Array of Object and check if Specific key has a value in an Array

How to get the value in object based on the largest key (Date is an Key)

how to get value from key in map if key is an object js

how to get the object key if the object and array value matches in javascript

How do I get a key value of each object in a javascript object?

How to get the corresponding key and value from the object and convert it to an array object

How to get value of a key inside an object inside another object

How to get a value from powershell where the key has a dot at the beginning?

How to get the three key/value pairs with the lowest value from an object?

How to check if an object has a key?