Javascript 如何将对象数组转换为序列化对象?
我有一个对象数组Javascript 如何将对象数组转换为序列化对象?,javascript,arrays,react-native,serialization,Javascript,Arrays,React Native,Serialization,我有一个对象数组 const parameters = [ {token: '78fe6df3f'}, {id: '12345'}, { price: '0 - 9,000,000' }, { 'area[]': 'Applehead Island' }, { 'waterfront_type[]': 'Open Water' }, { property_type_single: 'Single Family/Site Built' }, { bedrooms: '0
const parameters = [
{token: '78fe6df3f'},
{id: '12345'},
{ price: '0 - 9,000,000' },
{ 'area[]': 'Applehead Island' },
{ 'waterfront_type[]': 'Open Water' },
{ property_type_single: 'Single Family/Site Built' },
{ bedrooms: '0 - 5' },
{ baths: '0 - 5' },
{ sqft: '0 - 7500' }
];
我想把这个物体变成下面这样
https://www.example.com/properties.php?token=78fe6df3f&id=12345&price=$0%20-%20$3,480,000&area[]=Applehead%20Island&waterfront_type[]=Open%20Water&property_type_single=Single%20Family/Site%20Built&bedrooms=0%20-%205&baths=0%20-%205&sqft=0%20-%207500
请帮助了解如何获取此信息?假设这是JavaScript,您可以使用此函数对所有键值对进行URL编码,如图所示。只需在数组上迭代并连接URL编码的值:
const参数=[
{令牌:'78fe6df3f'},
{id:'12345'},
{价格:'0-9000000'},
{'area[]':'Applehead Island'},
{“滨水区”类型[]:“开放水域”},
{property_type_single:'单族/已建场地'},
{卧室:'0-5'},
{baths:'0-5'},
{sqft:'0-7500'}
];
让uri=”https://example.org/properties.php?";
让firstSegment=true;
for(参数常量参数){
如果(!firstSegment){
uri+=“&”;
第一段=假;
}
//查找此对象属性的名称
const paramName=Object.keys(param)[0];
uri+=paramName+“=”+encodeURIComponent(param[paramName]);
}
log(uri)代码>假设这是JavaScript,您可以使用该函数对所有键值对进行URL编码,如图所示。只需在数组上迭代并连接URL编码的值:
const参数=[
{令牌:'78fe6df3f'},
{id:'12345'},
{价格:'0-9000000'},
{'area[]':'Applehead Island'},
{“滨水区”类型[]:“开放水域”},
{property_type_single:'单族/已建场地'},
{卧室:'0-5'},
{baths:'0-5'},
{sqft:'0-7500'}
];
让uri=”https://example.org/properties.php?";
让firstSegment=true;
for(参数常量参数){
如果(!firstSegment){
uri+=“&”;
第一段=假;
}
//查找此对象属性的名称
const paramName=Object.keys(param)[0];
uri+=paramName+“=”+encodeURIComponent(param[paramName]);
}
log(uri)代码>您可以将所有对象合并为一个对象,检查其属性并将其合并为一个字符串:
const result = encodeURIComponent(Object.entries(Object.assign({}, ...parameters)).map(([key, value]) => key + "=" + value).join("&"));
逐步:
[{ k: v }, { k2, v2 }]
1) Object.assign({},…参数)
2) Object.entries(…)
3) .map(([key,value])=>key+“=”+value)
4) .join(&)
您可以将所有对象合并为一个对象,检查其属性并将其合并为一个字符串:
const result = encodeURIComponent(Object.entries(Object.assign({}, ...parameters)).map(([key, value]) => key + "=" + value).join("&"));
逐步:
[{ k: v }, { k2, v2 }]
1) Object.assign({},…参数)
2) Object.entries(…)
3) .map(([key,value])=>key+“=”+value)
4) .join(&)
我在stack overflow中的目标是以一种非常简单的方式解释概念。我已经在代码中进行了注释,因此您将理解每一步
${}
是ES6概念,如果您从未见过它们,请参考此
谢谢你挑战我
这是我解决的代码,简单明了
var parameters = [
{token: '78fe6df3f'},
{id: '12345'},
{ price: '0 - 9,000,000' },
{ 'area[]': 'Applehead Island' },
{ 'waterfront_type[]': 'Open Water' },
{ property_type_single: 'Single Family/Site Built' },
{ bedrooms: '0 - 5' },
{ baths: '0 - 5' },
{ sqft: '0 - 7500' }
];
//we initialize an empty variable
var serializedString = '';
//.map() This loop will loop through the array to pick each object
parameters.map((i)=>{
//for-in This loop goes through the key-value inside of each object
for(var key in i){
//here we are assigning the stringed values into the variable we declared earlier on
serializedString +=`${key}=${i[key]}&`;
}
});
//after all this is done , we convert our string into a URL friendly string
var ourURL_friendlyResult = encodeURIComponent(serializedString);
//we console it out
console.log(`https://example.org/properties.php?${ourURL_friendlyResult}`);
我在stack overflow中的目标是以一种非常简单的方式解释概念。我已经在代码中进行了注释,因此您将理解每一步
${}
是ES6概念,如果您从未见过它们,请参考此
谢谢你挑战我
这是我解决的代码,简单明了
var parameters = [
{token: '78fe6df3f'},
{id: '12345'},
{ price: '0 - 9,000,000' },
{ 'area[]': 'Applehead Island' },
{ 'waterfront_type[]': 'Open Water' },
{ property_type_single: 'Single Family/Site Built' },
{ bedrooms: '0 - 5' },
{ baths: '0 - 5' },
{ sqft: '0 - 7500' }
];
//we initialize an empty variable
var serializedString = '';
//.map() This loop will loop through the array to pick each object
parameters.map((i)=>{
//for-in This loop goes through the key-value inside of each object
for(var key in i){
//here we are assigning the stringed values into the variable we declared earlier on
serializedString +=`${key}=${i[key]}&`;
}
});
//after all this is done , we convert our string into a URL friendly string
var ourURL_friendlyResult = encodeURIComponent(serializedString);
//we console it out
console.log(`https://example.org/properties.php?${ourURL_friendlyResult}`);
这是什么语言?这是JavaScript吗?这是对象数组。我需要用它来获取调用。但是这个数组是用哪种语言定义的?恐怕我不理解你的问题。这是Javascript谢谢,这就是我要找的东西这是什么语言?这是JavaScript吗?这是对象数组。我需要用它来获取调用。但是这个数组是用哪种语言定义的?恐怕我不理解你的问题。谢谢,这就是我要找的
var parameters = [
{token: '78fe6df3f'},
{id: '12345'},
{ price: '0 - 9,000,000' },
{ 'area[]': 'Applehead Island' },
{ 'waterfront_type[]': 'Open Water' },
{ property_type_single: 'Single Family/Site Built' },
{ bedrooms: '0 - 5' },
{ baths: '0 - 5' },
{ sqft: '0 - 7500' }
];
//we initialize an empty variable
var serializedString = '';
//.map() This loop will loop through the array to pick each object
parameters.map((i)=>{
//for-in This loop goes through the key-value inside of each object
for(var key in i){
//here we are assigning the stringed values into the variable we declared earlier on
serializedString +=`${key}=${i[key]}&`;
}
});
//after all this is done , we convert our string into a URL friendly string
var ourURL_friendlyResult = encodeURIComponent(serializedString);
//we console it out
console.log(`https://example.org/properties.php?${ourURL_friendlyResult}`);