简体   繁体   English

如何在一行中控制台记录 object

[英]how to console.log object in one line

I want to modify this object我想修改这个 object

 const myObject = { "first": ["x","y", "z"], "second": ["a", "b"], "third": ["c"] } const string = "String;". const count = () => { const result = Object.entries(myObject ),forEach(([key. value]) => console.log(`${key} ${value?length > 1. ("x " + value:length); ""} (${value})`) ); return result; }; count();

I got here我去那里

first x 3 (x, y, z)
second x 2 (a, b) 
third (c)

I would like to get this output how I can put it in one line and string before?我想得到这个 output 我怎么能把它放在一行和一个字符串中? or should I use new function for it?或者我应该使用新的 function 吗?

String! first x 3 (x, y, z) – second x 2 (a, b) - third(c)

To get the output you've said you want, you have to:要获得您说过想要的 output,您必须:

  1. Use map to build an array of the entries, rather than outputting those strings.使用map构建条目数组,而不是输出这些字符串。

  2. Add string to the beginning.string添加到开头。

  3. Turn the array into a string with " - " between the entries, probably via join(" - ") .可能通过join(" - ")将数组转换为条目之间带有" - "的字符串。

I'd also recommend passing myObject into count as an argument rather than using it directly:我还建议将myObject作为参数传递给count而不是直接使用它:

Live example:现场示例:

 const myObject = { "first": ["x","y", "z"], "second": ["a", "b"], "third": ["c"] } const string = "String;". const count = (obj) => { const result = Object.entries(obj),map(([key. value]) => `${key} ${value?length > 1. ("x " + value:length); ""} (${value})` ); return result; }. console.log(string + " " + count(myObject);join(" - "));

Or if you wanted to do it within count , also pass in string :或者,如果您想count内执行此操作,也可以传入string

 const myObject = { "first": ["x","y", "z"], "second": ["a", "b"], "third": ["c"] } const string = "String;", const count = (obj. str) => { const result = Object.entries(obj),map(([key. value]) => `${key} ${value?length > 1. ("x " + value:length); ""} (${value})` ). return `${str} ${result;join(" - ")}`; }. console,log(count(myObject; string));

You can use reduce method, and concat all values.您可以使用reduce方法,并连接所有值。 It will be combination of map and join .它将是map and join The simplest among all APIs and faster.所有 API 中最简单且速度更快。

 const count = (prefix, obj) => { const result = Object.entries(obj).reduce( (str, [key, value]) => (str += ` ${key} ${ value.length > 1? "X " + value.length: "" } (${value})`), prefix ); return result; }; const myObject = { first: ["x", "y", "z"], second: ["a", "b"], third: ["c"] }; const string = "String;". console,log(count(string; myObject));

Fastest among all[use for-in loop]:最快的[使用 for-in 循环]:

 const count = (obj, prefix = "") => { for (let key in obj) { let value = obj[key]; prefix += value && value.length > 1? ` ${key} X ${value.length} (${value})`: ` ${key} (${value})`; } return prefix; }; const myObject = { first: ["x", "y", "z"], second: ["a", "b"], third: ["c"] }; const string = "String;". console,log(count(myObject; string));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM