[英]Printing object's keys and values
I want to print a key: value pair from javascript object.我想从 javascript 对象打印一个 key: value 对。 I can have different keys in my array so cannot hardcode it to object[0].key1
我的数组中可以有不同的键,因此不能将其硬编码为 object[0].key1
var filters = [{"user":"abc"},{"application":"xyz"}];
console.log(Object.keys(filters[0])[0]); // prints user
var term = (Object.keys(filters[0])[0]);
console.log(filters[0].term); // prints undefined
How can i print the value of the key我如何打印密钥的值
for (key in filters[0]){
console.log( key + ": " + filters[0][key]);
}
Or if you want to print all the values of filters或者如果你想打印过滤器的所有值
for (i in filters){
console.log(i);
for (key in filters[i]){
console.log( key + ": " + filters[i][key]);
}
}
filters.forEach(function(obj, index){
console.log(index);
for (var key in obj){
console.log(key, obj[key]);
}
});
This is looking for a term
property on filters[0]
:这是在
filters[0]
上寻找term
属性:
console.log(filters[0].term);
What you actually want to do is use the value of term
(in your example that will be "user"
) as the property identifier:您真正想要做的是使用
term
的值(在您的示例中为"user"
)作为属性标识符:
console.log(filters[0][term]);
for loop for array and for..in iteration for object:数组的 for 循环和对象的 for..in 迭代:
var filters = [{ "user": "abc"}, {"application": "xyz"}]; for (var i = 0; i < filters.length; i++) { // the plainest of array loops var obj = filters[i]; // for..in object iteration will set the key for each pair // and the value is in obj[key] for (var key in obj) { console.log(key, obj[key]) } }
ES6 ES6
[{ "user": "abc"}, {"application": "xyz"}].forEach( obj => console.log(Object.entries(obj).flat()) )
You can access the value using array syntax您可以使用数组语法访问该值
var filters = [{"user":"abc"},{"application":"xyz"}];
console.log(Object.keys(filters[0])[0]); // prints user
var term = (Object.keys(filters[0])[0]);
console.log(filters[0][term]);// prints abc
Lets say that we have a mode object that has some strings in it for example.假设我们有一个模式对象,其中包含一些字符串。 If we were to do
MODE.toString()
with just alpha, beta, gamma in the object, what will be returned is [object Object]
which is not useful.如果我们在对象中只使用 alpha、beta、gamma 来执行
MODE.toString()
,那么将返回的是[object Object]
这没有用。
Instead, lets say we wanted to get something nice back like Normal, Sepia, Psychedelic
.相反,假设我们想要得到一些不错的东西,比如
Normal, Sepia, Psychedelic
。 To do that, we could add a toString: function(){...}
to our object that will do just that.要做到这一点,我们可以向我们的对象添加一个
toString: function(){...}
来做到这一点。 One catch to this however is that if we loop through everything in the object, the function it self will also be printed, so we need to check for that.然而,一个问题是,如果我们循环遍历对象中的所有内容,它自己的函数也将被打印,因此我们需要检查它。 In the example I'll check
toString
specifically, however, other checks like ... && typeof MODE[key] == "string"
could be used instead在示例中,我将专门检查
toString
,但是,可以使用其他检查,例如... && typeof MODE[key] == "string"
Following is some example code, calling MODE.toString();
以下是一些示例代码,调用
MODE.toString();
will return Normal, Sepia, Psychedelic
将返回
Normal, Sepia, Psychedelic
var MODE = {alpha:"Normal", beta:"Sepia", gamma:"Psychedelic",
toString: function() {
var r = "";
for (var key in MODE) {
if (MODE.hasOwnProperty(key) && key != "toString") {
r+= r !== "" ? ", ":"";
r+= MODE[key];
}
}
return r;
}
};
if you want get all keys in array of object, you can try this one mybe如果你想获取对象数组中的所有键,你可以试试这个 mybe
let temp = []
let keys = []
let result = []
for (let i = 0; i < data.length; i++) {
temp = Object.keys(data[i])
for (let j = 0; j < temp.length; j++) {
if(!keys.includes(temp[j])){
keys.push(temp[j])
}
}
temp = []
}
for (let i = 0; i < data.length; i++) {
for (let j = 0; j < keys.length; j++) {
if(data[i][keys[j]] == undefined){
data[i][keys[j]] = ""
}
}
}
return data
or this one if you want take the key from same array 2dimension或者这个,如果你想从同一个二维数组中获取密钥
function convertObj(arr){
let arrResult = []
for (let i = 1; i < arr.length; i++) {
let obj={}
for (let j = 0; j < arr[0].length; j++) {
obj[arr[0][j]] = arr[i][j]
}
arrResult.push(obj)
}
return arrResult
}
If you want to print key and value, even for nested object then you can try this function:如果你想打印键和值,即使是嵌套对象,你也可以试试这个函数:
function printObjectData(obj, n = 0){
let i = 0;
var properties = Object.keys(obj);
let tab = "";
for(i = 0; i < n; i++)
tab += "\t";
for(i = 0; i < properties.length; i++){
if(typeof(obj[properties[i]]) == "object"){
console.log(tab + properties[i] + ":");
printObjectData(obj[properties[i]], n + 1);
}
else
console.log(tab + properties[i] + " : " + obj[properties[i]]);
}
}
printObjectData(filters);
and the solution will look like this:解决方案如下所示:
0:
user : abc
1:
application : xyz
and if you want to remove 0: and 1: then simply remove如果你想删除 0: 和 1: 然后简单地删除
console.log(tab + properties[i] + ":");
after the if statement在 if 语句之后
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.